Sub topic 5
DKIM: signing messages with your domain
This chapter assumes you can read raw email headers, edit DNS TXT records, and use a command line. It also assumes you know the basics of SPF from the previous chapter. Gmail, Yahoo, and Microsoft's consumer mailboxes expect bulk senders to authenticate with both SPF and DKIM. DKIM is the part that survives forwarding, and it is the usual route to a DMARC pass for marketing mail.
What DKIM proves
DomainKeys Identified Mail (DKIM, RFC 6376) lets a sending system attach a cryptographic signature to a message. The receiver fetches the matching public key from DNS and checks the signature. A pass proves two things:
- The signed headers and the body were not changed after signing.
- The signing domain (
d=) authorized the signature, because only someone holding the private key could produce it.
DKIM does not prove that the visible From address is honest. A message from [email protected] can carry a valid signature from d=attacker.test. Tying the signature to the From domain is the job of alignment, covered in the next chapter. For that reason, a bulk sender's goal is a signature whose d= matches the From domain.
How signing and verification work
The sender computes a hash of the canonicalized body and stores it in the bh= tag. It then hashes the selected headers plus the signature header itself (with an empty b=) and signs that hash with its private key. The result goes in b=. The receiver repeats the same steps with the public key. If any signed byte differs after canonicalization, verification fails.
Anatomy of a signature
A signature header looks like this (values shortened):
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com; s=mail1;
t=1764164611; h=from:to:subject:date:message-id:list-unsubscribe:list-unsubscribe-post;
bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
b=dzdVyOfAKCdLXdJOc9G2q8LoXSlEniSbav+yuU4zGeeruD00lszZVoG4ZHRNiYzR...| Tag | Meaning |
|---|---|
| `v=1` | DKIM version |
| `a=rsa-sha256` | Signing algorithm. Avoid the obsolete `rsa-sha1`. |
| `c=relaxed/relaxed` | Canonicalization for headers/body. Relaxed tolerates whitespace and header-case changes. |
| `d=` | Signing domain. This is the value DMARC compares to the From domain. |
| `s=` | Selector, which names the key |
| `h=` | Headers covered by the signature |
| `bh=` | Hash of the canonicalized body |
| `b=` | The signature itself |
The selector lets one domain publish many keys. The receiver looks up <selector>._domainkey.<d>, so s=mail1 with d=example.com means a query for mail1._domainkey.example.com. Different mail streams (marketing platform, transactional system, your corporate mail) can use different selectors, so each can rotate keys independently.
Publishing the key
Generate a 2048-bit RSA key pair. Google recommends 2048 bits, and 1024 bits is the practical minimum. Many ESPs generate the pair for you and give you a record to publish. If you manage your own keys, you can do it like this:
openssl genrsa -out mail1.private.pem 2048
openssl rsa -in mail1.private.pem -pubout -outform der 2>/dev/null | openssl base64 -AThe second command prints the public key as one base64 line. That line is the p= value. Publish it as a TXT record:
mail1._domainkey.example.com. TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...(public key from your sender)..."A single DNS string holds at most 255 characters, and a 2048-bit key is longer than that. Most DNS providers accept the long value and split it into several quoted strings for you. If yours does not, split it yourself: "v=DKIM1; k=rsa; p=MIIB..." "...rest of key". Resolvers join the strings with no separator.
Many ESPs ask you to publish a CNAME instead, such as mail1._domainkey.example.com CNAME mail1.dkim.esp.example.net. This lets the ESP rotate the key without a change on your side. The signature still uses d=example.com, so it still aligns.
Check that the record is live:
dig +short TXT mail1._domainkey.example.comIf the answer is empty, the receiver will report dkim=neutral or dkim=fail with a "no key for signature" reason, even though the signature is well formed.
Signing a message in code
If you send through your own software, a library does the canonicalization and hashing. This example uses the dkimpy package (pip install dkimpy) and the private key from above:
import dkim
message = (
b"From: Example News <[email protected]>\r\n"
b"To: [email protected]\r\n"
b"Subject: Our November update\r\n"
b"Date: Thu, 26 Nov 2026 13:43:31 +0000\r\n"
b"Message-ID: <[email protected]>\r\n"
b"List-Unsubscribe: <https://example.com/unsubscribe/abc123>\r\n"
b"List-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n"
b"\r\n"
b"Hello from the November update.\r\n"
)
with open("mail1.private.pem", "rb") as f:
private_key = f.read()
signature = dkim.sign(
message,
selector=b"mail1",
domain=b"example.com",
privkey=private_key,
include_headers=[
b"from", b"to", b"subject", b"date", b"message-id",
b"list-unsubscribe", b"list-unsubscribe-post",
],
canonicalize=(b"relaxed", b"relaxed"),
)
signed_message = signature + message
print(signed_message.decode())The returned value is the DKIM-Signature: header line. Prepend it to the message, as the last line does. The include_headers list matters. The From header must always be signed, because DKIM requires it. Signing list-unsubscribe and list-unsubscribe-post stops an intermediary from replacing your unsubscribe URL, and one-click unsubscribe (RFC 8058) requires that both headers be covered. The unsubscribe chapter builds on this.
Reading the result
The receiver reports its verdict in Authentication-Results. This sample comes from a message where DKIM passes and DMARC passes through DKIM alignment even though SPF used a different domain:
Authentication-Results: mx.example.net;
spf=pass smtp.mailfrom=bounce.example.com;
dkim=pass header.d=example.com header.s=mail1;
dmarc=pass (p=NONE) header.from=example.comheader.d is the signing domain and header.s is the selector. When troubleshooting, compare header.d with the From domain first. A dkim=pass with header.d=esp.example.net is a valid signature that does nothing for DMARC.
Common failure modes
- Signed by the ESP's domain only. Many platforms sign with their own domain until you complete a custom-domain setup. DKIM passes, but it is not aligned.
- Body modified after signing. Mailing lists that add footers, security gateways that append disclaimers, and link-rewriting tools that run after signing all change the body hash. Relaxed canonicalization forgives whitespace changes, not content changes. Do the rewriting before the signature step.
- Missing or truncated key. A DNS provider that silently cuts a long value, or a stray space inside
p=, produces a "key syntax error" or "bad signature" result. - Wrong selector. The
s=in the message must match a published record exactly. - Signature expiry. An
x=tag sets an expiry time. A message that sits in a queue past it fails verification. - Use of
l=. This tag limits the signed body length and lets an attacker append content. Do not use it. - Weak keys. Keys under 1024 bits may be treated as invalid by receivers.
Why DKIM matters more than SPF for marketing mail
SPF checks the connecting IP against the envelope sender, so forwarding and many ESP setups break it or leave it unaligned. DKIM travels with the message and checks out wherever it lands, as long as the signed parts stay intact. For bulk marketing mail sent through an ESP, a custom-domain DKIM signature is often the only reliable path to DMARC alignment.
Exercise
A marketing email shows spf=pass smtp.mailfrom=bounce.esp.example.net, dkim=pass header.d=esp.example.net header.s=s1, and a From address of [email protected]. Both checks pass. What is missing for the message to meet the bulk sender requirements, and which DNS record would you publish to fix it?
Show answer
Neither SPF nor DKIM is aligned with the From domain example.com, because the SPF domain and the DKIM d= are both the ESP's domain. A DMARC check would fail. The fix is to set up custom-domain signing with the ESP so it signs with d=example.com. That means publishing the key record the ESP provides at <selector>._domainkey.example.com, either as a TXT record with the public key or as a CNAME to the ESP. Then confirm with dig +short TXT <selector>._domainkey.example.com and check that new messages show dkim=pass header.d=example.com.