Nodemailer is able to sign all messages with DKIM keys. This means calculating a signature for the message and adding it as an additional header (or headers, if you use multiple keys) to the message.
The drawback on DKIM signing is that Nodemailer needs to cache the entire message before it can be sent, unlike normal sending where message output is streamed to SMTP as it is created and nothing needs to be cached. For small messages it does not matter, for larger messages Nodemailer offers an option to cache messages not in memory but on disk. In this case Nodemailer starts buffering the message in memory and if the message size reaches a certain treshold, it gets directed to a file on disk. Once signature is calculated and sent to SMTP, Nodemailer streams the cached message from disk to SMTP.
In general DKIM signing should be fast and effective.
DKIM signing can be set on the transport level (all messages get signed with the same keys) and also on the message level (provide different keys for every message). If both are set, then message level DKIM configuration is preferred.
In both cases you need to provide a dkim
object with the following structure
message-id:date:from:to...'
)'message-id:date'
to prevent signing these values.Assumes that there is a public key available for 2017._domainkey.example.com. You can test if the key exists or not with the dig tool like this
dig TXT 2017._domainkey.example.com
let transporter = nodemailer.createTransport({
service: 'Gmail',
dkim: {
domainName: 'example.com',
keySelector: '2017',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...'
}
});
Assumes that there is a public keys available for 2017._domainkey.example.com and 2016._domainkey.example.com
let transporter = nodemailer.createTransport({
service: 'Gmail',
dkim: {
keys: [
{
domainName: 'example.com',
keySelector: '2017',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...'
},
{
domainName: 'example.com',
keySelector: '2016',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...'
}
]
cacheDir: false
}
});
Do not sign by default. Provide DKIM key values separately for every message.
let transporter = nodemailer.createTransport({
service: 'Gmail'
});
let message = {
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Message',
text: 'I hope this message gets read!',
dkim: {
domainName: 'example.com',
keySelector: '2017',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...'
}
};
Messages larger than 100kB are cached to disk
let transporter = nodemailer.createTransport({
service: 'Gmail',
dkim: {
domainName: 'example.com',
keySelector: '2017',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...',
cacheDir: '/tmp',
cacheTreshold: 100 * 1024
}
});
This is needed when sending mail through SES that has its own Message-ID and Date system.
let transporter = nodemailer.createTransport({
service: 'Gmail',
dkim: {
domainName: 'example.com',
keySelector: '2017',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...',
skipFields: 'message-id:date'
}
});