Sending emails has become a basic feature of developing mobile and web applications. Emails facilitate easy communication between the user and admins.
flutter_email_sender
is a Flutter package that allows easy email sending options for developers.
flutter_email_sender
allows the user to send email with the following features:
To use this package in a Flutter application, we need to install Flutter on our computer.
We also need to create a Flutter application using the following code:
flutter create send_email
cd send_email
flutter analyze
flutter test
flutter run lib/main.dart
We start writing our code for the application in the lib/main.dart
file.
In the beginning, the file looks as follows:
Our next step is to install flutter_email_sender
in our Flutter application with the following code:
flutter pub add flutter_email_sender
After we install the package, we can verify it from the pubspec.yaml
file, where the following line will be added:
dependencies:flutter_email_sender: ^5.0.2
Sometimes the editor needs to be restarted, or the following command should be run to complete installation:
flutter pub get
Now the package will be successfully installed and ready to use inside the code.
The next step is to import the package in our dart file where we need to write the code to send the email.
To import the package, add the following code to the top of the dart file:
import 'package:flutter_email_sender/flutter_email_sender.dart';
You need to create a Flutter screen that contains text fields of the email. This includes a list of recipients, email subject, and email contents, along with the option to add attachments.
At the end, add a button to send an email.
You have to make a structure for emails that contains all the data that needs to be sent in the email. The code is as follows:
final Email send_email = Email(body: 'body of email',subject: 'subject of email',recipients: ['example1@ex.com'],cc: ['example_cc@ex.com'],bcc: ['example_bcc@ex.com'],attachmentPaths: ['/path/to/email_attachment.zip'],isHTML: false,);await FlutterEmailSender.send(send_email);
In Android 11 smartphones, package visibility is launched and changes the packages on user devices. To enable this feature, you need to change your AndroidManifest.xml
file in your project.
Add the following code to the above-mentioned file.
<manifest package="com.companyname.appname"><queries><intent><action android:name="android.intent.action.SENDTO" /><data android:scheme="mailto" /></intent></queries></manifest>