23rd Oct 2018

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


Author

Mayank Bajaj

Software Engineer

3 min readMobile Applications
3 min readMobile Applications

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:

dependencies:
  flutter:
    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')
    ..recipients.add('destination@example.com')
    ..ccRecipients.addAll(['destCc1@example.com', 'destCc2@example.com'])
    ..bccRecipients.add(new Address('bccAddress@example.com'))
    ..subject = 'Test Dart Mailer library :: 😀 :: ${new DateTime.now()}'
    ..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.

We bring the best in technology combined with an Agile process which results in higher efficiency and desired results for our clients.

Talk to us!

LATEST FROM OUR BLOG

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.

Read more

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.

Read more

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.

Read more