send method
Implementation
Future<mailer.SendReport> send() async {
final message = mailer.Message();
message.from = envelope().from ??
Address(
env<String>('MAIL_FROM_ADDRESS', ''),
env<String>('MAIL_FROM_NAME', ''),
);
message.recipients.addAll(envelope().to);
if (envelope().cc != null) {
message.ccRecipients.addAll(envelope().cc!);
}
if (envelope().bcc != null) {
message.ccRecipients.addAll(envelope().bcc!);
}
message.subject = envelope().subject;
MailView? mailView = view();
Content? contentData = content();
if (mailView != null) {
message.html = TemplateEngine().render(
mailView.view,
mailView.data ?? {},
);
} else if (contentData != null) {
message.text = contentData.text;
message.html = contentData.html;
}
if (attachments() != null) {
message.attachments.addAll(attachments()!);
}
try {
mailer.SendReport sendReport = await mailer.send(
message,
_setupSmtpServer(),
);
return sendReport;
} on SmtpMessageValidationException catch (e) {
stderr.writeln('Failed to send email:${e.problems.map((error) => {
message: error.msg,
error: error.code
}).toList()}');
throw Exception(e.problems
.map((error) => {message: error.msg, error: error.code})
.toList());
} catch (e) {
stderr.writeln('Failed to send email: $e');
rethrow;
}
}