Skip to content

DKIM signing

DKIM lets the receiving servers check that an email was sent by a server allowed to send for its domain. The nodemailer output signs the emails it sends with the keys in its dkims option. This guide creates a key, publishes it, and configures the output.

The dkim-generate command creates a 2048-bit RSA key pair. Run it with the Docker image:

Terminal window
docker run --rm loopingz/smtp-relay:latest dkim-generate example.com default

or from a clone, after npm ci && npm run build:

Terminal window
node lib/index.js dkim-generate example.com default

The arguments are the domain and the selector, a name you choose to identify the key (default, or a date such as 202611 to rotate keys later).

The command first prints the DNS record:

Create a DNS TXT record with the following value:
default._domainkey.example.com v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA…IDAQAB

Create a TXT record named default._domainkey.example.com (<selector>._domainkey.<domain>) with the value v=DKIM1; k=rsa; p=…, copied whole. Some DNS providers split long values into several strings: that is expected.

The command then prints the private key inside two JSON snippets. They nest the key under nodemailer, which is not where the output reads it: copy only the privateKey value, and put it in the output’s dkims option, keyed by domain:

dkim.jsonc
{
"$schema": "https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json",
"flows": {
"outgoing": {
"filters": [{ "type": "whitelist", "subnets": ["10.0.0.0/8"] }],
"outputs": [
{
"type": "nodemailer",
"nodemailer": { "host": "smtp.provider.example", "port": 587, "auth": { "user": "relay", "pass": "secret" } },
"dkims": {
"example.com": {
"keySelector": "default",
"privateKey": "-----BEGIN PRIVATE KEY-----\nMIIEvQ...\n-----END PRIVATE KEY-----\n"
}
}
}
]
}
},
"bind": "0.0.0.0",
"options": { "authOptional": true }
}
  • The key is selected by the domain of the envelope sender (MAIL FROM): an email sent by alice@example.com is signed with the example.com entry. Add one entry per domain you send for; emails from other domains are sent unsigned.
  • In JSON, the PEM key is one string with \n for the line breaks, as the command prints it. In YAML, a block scalar (privateKey: |) keeps the lines as they are.
  • The private key signs emails for your domain: keep it out of version control and give it to the server as a secret, for example by generating the configuration file at deployment.

Send an email through the relay to a mailbox you can read, and look at its Authentication-Results header: it should contain dkim=pass with header.d=example.com. An smtp-relay with the mail-auth filter and a file output shows the same header in the stored email.