Nodemailer SES transport is a wrapper around aws.SES from the @aws-sdk/client-ses package. SES transport is available from Nodemailer v3.1.0.
The SES API exposes two methods to send mail – SendEmail and SendRawEmail. While the first one is really easy to use and Nodemailer is not actually needed, then it is also quite basic in features. For example you can’t use attachments with SendEmail. On the other hand SendRawEmail requires you build your own MIME formatted email message which is far from being easy. And this is where Nodemailer steps in – it gives you a simple to use API while supporting even really complex scenarios like embedded images or calendar events.
In addition to the simple API, Nodemailer also provides rate limiting for SES out of the box. SES can tolerate short spikes but you can’t really flush all your emails at once and expect these to be delivered. To overcome this you can set a rate limiting value and let Nodemailer handle everything – if too many messages are being delivered then Nodemailer buffers these until there is an opportunity to do the actual delivery.
To use SES transport, set a aws.SES object as the value for SES property in Nodemailer transport options. That’s it. You are responsible of initializing that object yourself as Nodemailer does not touch the AWS settings in any way.
Additional properties to use are the following:
If you use rate or connection limiting then you can also use helper methods to detect if the sending queue is full or not. This would help to avoid buffering up too many messages.
Listen for the ‘idle’ event to be notified if you can push more messages to the transporter. To explicitly check if there are free spots available use isIdle() method (see example #2).
When sending mail there’s also an additional message option for setting SendRawEmail options
The info argument for sendMail() callback includes the following properties:
1. Not allowed to send messages
@aws-sdk/credential-provider-node
to be passed explicitly to the client constructor. Make sure to install the @aws-sdk/credential-provider-node module separately.2. aws-sdk is not found
You need to install the @aws-sdk/client-ses module separately, it is not bundled with Nodemailer.
let nodemailer = require("nodemailer");
let aws = require("@aws-sdk/client-ses");
let { defaultProvider } = require("@aws-sdk/credential-provider-node");
const ses = new aws.SES({
apiVersion: "2010-12-01",
region: "us-east-1",
defaultProvider,
});
// create Nodemailer SES transporter
let transporter = nodemailer.createTransport({
SES: { ses, aws },
});
// send some mail
transporter.sendMail(
{
from: "sender@example.com",
to: "recipient@example.com",
subject: "Message",
text: "I hope this message gets sent!",
ses: {
// optional extra arguments for SendRawEmail
Tags: [
{
Name: "tag_name",
Value: "tag_value",
},
],
},
},
(err, info) => {
console.log(info.envelope);
console.log(info.messageId);
}
);
let transporter = nodemailer.createTransport({
SES: { ses, aws },
sendingRate: 1 // max 1 messages/second
});
// Push next messages to Nodemailer
transporter.once('idle', () => {
if (transporter.isIdle()) {
transporter.sendMail(...);
}
});
Nodemailer SES transport requires ses:SendRawEmail role
{
"Statement": [
{
"Effect": "Allow",
"Action": "ses:SendRawEmail",
"Resource": "*"
}
]
}