How to Send Email from Flutter App

How to Send Email from Flutter app

How to Send Email from Flutter app – By integrating the Flutter Mailer plugin, programmers may make their Flutter apps capable of sending email. It offers a straightforward user interface for SMTP email sending, the universal internet standard.

Flutter Mailer allows programmers to easily send emails with attachments, personalize the email subject and body, and deliver to a specific email address. The plugin allows developers to send emails containing HTML content to many recipients.

In addition to working on both iOS and Android, Flutter Mailer also offers a unified interface for sending emails on both platforms. Also, since it’s open source, programmers can alter it to work with their own systems.

How-to-Send-Email-from-Flutter-app

Add Flutter Mailer to your pubspec.yaml file and import it into your code to utilize it in your send Flutter app. Your program can then send emails using the Mailer class.

Basic Steps to Send Email from Flutter App using mailer:

  • First, add the mailer dependency to your Flutter project by adding the following line to your pubspec.yaml file:
dependencies:
  mailer: ^6.0.0
  • Next, import the necessary packages in your Flutter code:
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
  • Create a function to send the email:
Future sendEmail() async {
  final smtpServer = gmail('[email protected]', 'password');
  final message = Message()
    ..from = Address('[email protected]', 'Your Name')
    ..recipients.add('[email protected]')
    ..subject = 'Test Email'
    ..text = 'This is a test email sent from Flutter using mailer.';
  try {
    final sendReport = await send(message, smtpServer);
    print('Message sent: ' + sendReport.toString());
  } on MailerException catch (e) {
    print('Message not sent: ' + e.toString());
  }
}
  • Call the sendEmail() function to send the email:
sendEmail();

Note: Dont forgot to replace the ‘[email protected]’ and ‘yourpassword’ with your own Gmail account credentials, and ‘[email protected]’ with the email address of the recipient.

complete code:

sample code that you can modify with your own credentials to send emails.

import 'package:flutter/material.dart';
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';

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

class FlutterFluxApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Mailer Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Mailer Demo'),
        ),
        body: Center(
          child: RaisedButton(
            child: Text('Send Email'),
            onPressed: () {
              sendEmail();
            },
          ),
        ),
      ),
    );
  }

  Future sendEmail() async {
    final smtpServer = gmail('[email protected]', 'password');
    final message = Message()
      ..from = Address('[email protected]', 'Your Name')
      ..recipients.add('[email protected]')
      ..subject = 'Test Email'
      ..text = 'This is a test email sent from Flutter using mailer.';
    try {
      final sendReport = await send(message, smtpServer);
      print('Message sent: ' + sendReport.toString());
    } on MailerException catch (e) {
      print('Message not sent: ' + e.toString());
    }
  }
}

Send Email from Flutter App Using URL launcher

To Send Email from Flutter App using the URL launcher package in Flutter, you can use the following code:

import 'package:url_launcher/url_launcher.dart';

final Uri _emailLaunchUri = Uri(
    scheme: 'mailto',
    path: '[email protected]',
    queryParameters: {
        'subject': 'Hello from Flutter',
        'body': 'This is a test email from Flutter.'
    }
);

void _sendEmail() async {
    if (await canLaunch(_emailLaunchUri.toString())) {
        await launch(_emailLaunchUri.toString());
    } else {
        throw 'Could not launch $_emailLaunchUri';
    }
}

This code creates a Uri object with the email address, topic, and body to send. If the device can handle this URL, we utilize the canLaunch method to launch it.

This code requires the pubspec.yaml package url_launcher.

Complete example:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

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

class FlutterFluxApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Send Email Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final Uri _emailLaunchUri = Uri(
    scheme: 'mailto',
    path: '[email protected]',
    queryParameters: {
      'subject': 'Hello from FlutterFlux', // subject recipients
      'body': 'This is a test email from FlutterFlux.'
    }
  );

  Future _sendEmail() async {
    if (await canLaunch(_emailLaunchUri.toString())) {
      await launch(_emailLaunchUri.toString());
    } else {
      throw 'Could not launch $_emailLaunchUri';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(children: [
          Image.asset(
            'assets/logo.png',
            height: 30,
          ),
          Text('flutterflux.com')
        ]),
      ),
      body: Center(
        child: TextButton(
          child: Text('Send Email'),
          onPressed: _sendEmail,
        ),
      ),
    );
  }
}

In this example, we create a MyHomePage widget that contains a TextButton to trigger sending the email. We also define the _emailLaunchUri and _sendEmail methods inside the widget.

When the button is pressed, the _sendEmail method checks if the device can handle the email URL, and if it can, it launches the URL using the launch method.

Note that you still need to add the url_launcher package to your pubspec.yaml file for this code to work.

Conclusion

Flutter can Send Email from Flutter App using Dart mailer. The package’s API makes email sending straightforward. The package offers configuration options for email topic, body, and recipient.

Add the mailer package to your app’s dependencies in pubspec.yaml and import it in Dart to utilize it. After importing the package, make an instance of the SmtpServer class and configure it with your email server settings. Set the email’s topic, body, and recipient by creating an instance of the Message class. Then send the email.

Send Email from Flutter App, however your email server may demand user authorization and authentication. Handle email sending problems as well. read too How to get data from Firestore in Flutter

Hello, I'm Cakra. I'm currently occupied with developing an application using the Flutter framework. Additionally, I'm also working on writing some articles related to it.

You May Also Like