How to Send an Email from Flutter - SMS App Part 3

AuthorMayank Bajaj (Software Engineer)23rd Oct 2018 . 3 min read time

Up till now we have imported the sms package and started a listener to listen for incoming sms messages on the user’s phone.

> Now we will set up a mailer via Mailgun ( Create Mailgun account from here), and mail the incoming sms message to a particular email address.

In pubspec.yaml, append mailer: ^2.0.2 to the dependencies list:

    sdk: flutter
sms: ^0.2.0
mailer: ^2.0.2   # add this line

Now run flutter packages get in the console. This pulls the package into your project. You should see the following in the console:

flutter packages get
Running "flutter packages get" in app_name...
Process finished with exit code 0

In lib/main.dart, import the new mailer package:

import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server/mailgun.dart';

> Enter your Mailgun credentials, here.

String username = YOUR_MAILGUN_USERNAME;
String password = PASSWORD;
final smtpServer = mailgun(username, password);

Create an SMTP Server by calling the mailgun's function built in the mailer package.

final smtpServer = mailgun(username, password);

Composing your message:

final message = new Message()
    ..from = new Address(username, 'Your name')
    ..ccRecipients.addAll(['', ''])
    ..bccRecipients.add(new Address(''))
    ..subject = 'Test Dart Mailer library :: 😀 :: ${new}'
    ..text = 'This is the plain text.\nThis is line 2 of the text part.'
    ..html = &quot;Test\n<p>Hey! Here's some HTML content</p>&quot;;

So we are done with initializing the SMTP server and composing the message, Now its time to send the mail.

final sendReports = await send(message, smtpServer);

This will send the mail as we composed in the earlier step. You can edit the message and sender as per your convenience.


6 min

Building a Facebook Echo-Bot in Python

Gartner estimates that by 2020, chatbots will be handling 85 percent of customer-service interactions; they are already handling about 30 percent of transactions now.

View Article
3 min

How to Send an Email from Flutter - SMS App Part 3

In this blog, we will see how we can send emails from our flutter application using mailgun credentials. This post is in continuation of the Flutter SMS app series.

View Article
4 min

How To Listen for SMS using Flutter - SMS App Part 2

In continuation with the previous blog, we would continue to build our new basic SMS app using Flutter. In this blog, we would see how we can add the functionality of listening for incoming SMS messages on the device.

View Article

contact us

Sent!You are a bot!?