Send email through Mailgun API
- Add dependency
dependencies:
mailgun: ^0.1.0- Initialize mailer instance
import 'package:mailgun/mailgun.dart';
var mailgun = MailgunMailer(domain: "my-mailgun-domain", apiKey: "my-mailgun-api-key");- Send plain text email
var response = await mailgun.send(
from: from,
to: to,
subject: "Test email",
text: "Hello World");- Send HTML email
var response = await mailgun.send(
from: from,
to: to,
subject: "Test email",
html: "<strong>Hello World</strong>");- Send email using template and template's variables
var response = await mailgun.send(
from: from,
to: to,
subject: "Test email",
template: "my-template",
options: {
'template_variables': {
'author': 'John'
}
});- Send email with attachments
var file = new File('photo.jpg');
var response = await mailgun.send(
from: from,
to: to,
subject: "Test email",
html: "Please check my <strong>attachment</strong>",
attachments: [file]);Below are possible statuses of response.status:
SendResponseStatus.OK: mail is sent successfullySendResponseStatus.QUEUED: mail is added to queue, for example, mailgun is not delivered mail immediatelySendResponseStatus.FAIL: failed to send email
In case of failure, error's message is under response.message