Learn Flutter Step by Step
Posted on
< Previous
The mailer package allows you to send emails using Simple Mail Transfer Protocol (SMTP), which is a standard protocol used to send emails over the internet.
To use the mailer package, you need to provide your email address and password to authenticate your account with the SMTP server. You can then create a Message object with the email details, such as the sender, recipient, subject, and body of the email.
Once you have created the Message object, you can use the send method to send the email. The send method takes the Message object and the SMTP server configuration as parameters, and returns a SendReport object that contains information about the email delivery status.
Add the "mailer" package to your pubspec.yaml file:
Example:
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
void sendEmail() async {
final smtpServer = gmail("your-email-address", "your-password");
final message = Message()
..from = Address("your-email-address", "your-name")
..recipients.add("recipient-email-address")
..subject = "email-subject"
..text = "email-body";
try {
final sendReport = await send(message, smtpServer);
print('Message sent: ' + sendReport.toString());
} on MailerException catch (e) {
print('Message not sent. \n' + e.toString());
}
}
In this example,
The gmail method is used to configure the SMTP server with your Gmail account. The Message object is created with the email details, and the send method is used to send the email. The delivery status is printed to the console, and any errors are caught with a MailerException.
< Previous