DKIM Explained
Understand how DKIM signs email with a key published in DNS, how to set it up, rotate keys and diagnose failed signatures.
Key takeaways
- DKIM uses a private key to sign mail and a public key in DNS to verify it.
- The signature names a domain and a selector, which together tell the receiver where to find the key.
- A valid signature survives ordinary forwarding but breaks if signed headers or body text are altered.
- DKIM helps DMARC only when the signing domain matches the domain in the visible From address.
- Rotate keys by adding a new selector first, switching signing to it and retiring the old one later.
DKIM, short for DomainKeys Identified Mail, adds a digital signature to outgoing email. The sending system signs parts of the message with a private key, and the receiver checks the signature using a public key published in the sender's DNS. If it verifies, the receiver knows that the signing domain took responsibility for the message and that the signed parts have not changed since. The standard is RFC 6376.
To use it, you turn on signing at whatever service sends your mail and publish one or more DNS records it gives you. The rest of this guide explains what those records mean and what to check when signatures fail.
The problem it solves
Basic email lets any server claim to send from any address. SPF restricts which IP addresses may use a domain in the envelope, but it says nothing about the message itself and often fails after forwarding.
DKIM attaches proof to the message. The proof travels with it through relays, and anyone receiving it can check it against DNS. It relies on public-key cryptography: what the private key signs, the matching public key can verify, and publishing the public key doesn't let anyone forge signatures. Symmetric vs. public-key encryption explains that idea.
Note what DKIM does not do. It does not encrypt anything. It does not say the content is true or wanted. A spammer can sign mail with their own domain perfectly well.
What the signature looks like
The signature is a header added to the message:
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=example.com; s=mail2026; t=1796476800;
h=from:to:subject:date:message-id:content-type;
bh=(hash of the body);
b=(the signature itself) Other tags include c=, which sets canonicalization, and optional timestamps t= and x= for creation and expiry.
Canonicalization
Mail systems sometimes make harmless changes, such as rewrapping a header or trimming trailing spaces. Canonicalization defines how text is normalized before hashing. The simple method tolerates almost nothing. The relaxed method ignores certain whitespace and header name case differences. c=relaxed/relaxed, covering headers then body, is a common practical choice.
The length tag
An optional l= tag signs only the first part of the body. That lets content be appended without breaking the signature, which an abuser can exploit. Avoid using it.
The DNS record
The receiver builds the lookup name from the selector, the fixed label _domainkey and the signing domain:
mail2026._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkq..." p= holds the public key in base64. k= names the key type. An empty p= means the key has been revoked. A t=y flag marks a domain as testing.
You can look it up yourself:
dig mail2026._domainkey.example.com TXT +short Resolve-DnsName mail2026._domainkey.example.com -Type TXT A single string inside a TXT record is limited to 255 characters, and a 2048-bit RSA public key is longer. The record is then written as several quoted strings in one record, which verifiers join. Many DNS dashboards do this automatically; some require you to split it.
Hosted services often ask you to publish a CNAME instead, pointing your selector name at a record they control. That lets them rotate keys without further work from you.
Key types and sizes
RSA with SHA-256 is the widely supported baseline. RFC 8301 retired the old SHA-1 variant and says signers must use keys of at least 1024 bits and should use at least 2048. Use 2048 unless a system truly cannot. RFC 8463 added Ed25519 signatures, which have much shorter keys, but receiver support is less universal, so senders who use it generally sign with RSA as well.
Setting it up
- List every system that sends as your domain. Each needs its own signing setup.
- In each service, find the domain authentication or DKIM page. It will generate a key pair or let you supply one.
- Publish the records exactly as shown. Watch for DNS panels that append your domain twice.
- Wait for caches, then verify with a lookup.
- Enable signing after the record is visible.
- Send a test and read the receiver's
Authentication-Resultsheader. You wantdkim=passwithheader.d=set to your domain. - Publish a DMARC record and watch the reports.
If a service signs only with its own domain by default, the message may pass DKIM and still fail DMARC alignment. Turn on custom domain signing.
Rotating keys
Keys should be replaced from time to time, and immediately if a private key may have leaked. Selectors make this painless.
- Create a new key under a new selector.
- Publish its public record and confirm it resolves.
- Switch signing to the new selector.
- Leave the old public record in place for a while, so messages still in queues can be verified.
- Then revoke it with an empty
p=, or remove it.
Never reuse a selector name for a new key; cached copies of the old record would cause failures.
How verification works, step by step
When the receiver gets a signed message, it follows the signature's own instructions.
- It reads the tags and checks that the From header is among the signed fields, which the standard requires.
- It normalizes the body according to the chosen canonicalization, hashes it and compares the result with
bh=. A mismatch means the body changed, and verification stops there. - It builds the DNS name from
s=andd=and fetches the public key. - It normalizes the listed headers, along with the signature header itself with the
b=value left empty. - It uses the public key to check
b=against those headers. The body hash is part of the signed signature header, so a good result covers both.
The outcome is typically pass, fail, temperror for a lookup that may succeed later, or permerror for a signature or key that can never be processed. A message may carry several signatures, for example one from your domain and one from the sending service. Each is judged separately, and DMARC needs only one passing signature that aligns.
A failed signature is generally treated much like having no signature. It is a reason for suspicion only in combination with other evidence, since innocent modifications cause many failures.
Choosing selectors and planning ownership
Selectors are labels you pick, or your vendor picks. Names that carry meaning make later cleanup far easier: something identifying the service and the period is better than s1 and s2 with no notes.
| Practice | Why it helps |
|---|---|
| One selector and key per sending system | A leak or departure affects only that system |
| Separate keys for separate domains | One compromised key doesn't cover everything |
| A written list of selectors and owners | You know which records are safe to delete |
| Rotation on a calendar | The process stays practiced before an emergency |
Signing keys on your own server need file permissions that only the signing process can read. Back them up as carefully as other secrets, or accept that losing one simply means generating a new key and selector, which is usually the cleaner choice.
If a key is exposed, generate a replacement, switch signing, and revoke the old record promptly without waiting out the usual overlap. Some queued mail may then fail verification, which is a smaller problem than allowing others to sign as you.
Testing a new setup
After enabling signing, test each path separately: a message typed by a person, an automated notice from your website, and a campaign from your marketing tool. They often leave through different systems, and it is common for one of them to remain unsigned for months. Include a message with an attachment and one that passes through any outbound gateway, since those are where modifications happen. Check the d= domain every time, not only the word pass.
Why signatures fail
| Symptom | Likely cause |
|---|---|
| Key not found | Record on the wrong name, or not yet visible |
| Body hash mismatch | Something changed the body after signing |
| Signature mismatch | A signed header changed, or the wrong key is published |
| Bad key format | Pasted key truncated, or extra quotes and spaces |
| Expired | The x= time passed before verification |
| Works direct, fails via list | The list added a footer or subject prefix |
Gateways that append disclaimers are a frequent culprit. Make sure signing happens last, after any modification.
Mailing lists change messages by design. Some lists handle this by rewriting the From address, and ARC headers can pass along earlier results for receivers that choose to trust them.
Replay and other limits
A valid signature remains valid when someone copies the message and sends it to many other people, since DKIM doesn't bind to the envelope recipient. This is called replay. Senders reduce exposure by signing the From, To, Subject and Date fields, listing some headers an extra time so more cannot be added unnoticed, and setting sensible expiry times.
Protect private keys like any secret: restrict access, keep them off shared drives, and remove keys belonging to services you've left, along with their DNS records. A forgotten CNAME pointing at a former vendor leaves them, or whoever controls that name later, able to publish keys under your domain.
Common questions
Can one domain have several keys?
Yes. Each selector is independent, so every sending service can have its own, and rotation can overlap.
Do receivers reject unsigned mail?
Policies vary. Large mailbox providers increasingly expect authentication, particularly from bulk senders. See bulk sender requirements.
Where do I see the result?
In the received message's headers. Reading email headers shows where.