Customized email in Flutter to users using mailer package

 Follow the steps ;

  • get mailer from 'pub.dev'. 
                     Go to pub.dev: https://pub.dev/packages/mailer/install
  • get the dependencies by copying from mailer installing process and pasting it in pubspec.yaml
  • and then import the mailer package in main file or required files
                    
  • copy the function from mailer and paste it in main file.
                    


For live demo, checkout this youtube video:


FULL CODE:
    
    main.dart:
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Youtube Test App'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {

sendMail() async {
String username = 'youremailid@gmail.com';
String password = 'enteryourpassword';

final smtpServer = gmail(username, password);
final message = Message()
..from = Address(username)
..recipients.add('recipient@gmail.com')
// ..ccRecipients.addAll(['destCc1@example.com', 'destCc2@example.com'])
// ..bccRecipients.add(Address('bccAddress@example.com'))
..subject = 'Mail using mailer package :: 😀 :: ${DateTime.now()}'
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html = "<h1>Write the content here</h1>\n<p>Hey! its easy use html tags for alignments</p>";

try {
final sendReport = await send(message, smtpServer);
print('Message sent: ' + sendReport.toString());
Toast.show("You have clicked the Button! and email sent", context, duration: 3, gravity: Toast.CENTER);
} on MailerException catch (e) {
print('Message not sent.');
for (var p in e.problems) {
print('Problem: ${p.code}: ${p.msg}');
}
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: MaterialButton(
onPressed: sendMail,
splashColor: Colors.blueGrey,
color: Colors.blue,
child: Text('click',style: TextStyle(color: Colors.black,
fontSize: 20.0),
),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}

} 


Pubspec.yaml:

 name: youtubetest

description: Youtube test application

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
sdk: ">=2.1.0 <3.0.0"

dependencies:
mailer: ^3.2.1
toast: ^0.1.5
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2

dev_dependencies:
flutter_test:
sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages

# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages

Comments