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.
1. Generate a key pair
Section titled “1. Generate a key pair”The dkim-generate command creates a 2048-bit RSA key pair. Run it with the Docker image:
docker run --rm loopingz/smtp-relay:latest dkim-generate example.com defaultor from a clone, after npm ci && npm run build:
node lib/index.js dkim-generate example.com defaultThe 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).
2. Publish the public key
Section titled “2. Publish the public key”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…IDAQABCreate 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.
3. Configure the output
Section titled “3. Configure the output”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:
{ "$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 byalice@example.comis signed with theexample.comentry. Add one entry per domain you send for; emails from other domains are sent unsigned. - In JSON, the PEM key is one string with
\nfor 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.
4. Check the signature
Section titled “4. Check the signature”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.