What is DKIM? (DomainKeys Identified Mail)
DKIM (DomainKeys Identified Mail) is an email authentication standard that lets a sending server attach a cryptographic signature to each message, which the receiving server verifies against a public key published in the sending domain’s DNS — proving the message was authorised by the domain and was not altered in transit.
How DKIM works
When a server sends a DKIM-signed message, it hashes selected headers and the body, encrypts that hash with a private key, and attaches the result as a DKIM-Signature header. The matching public key lives in the sending domain’s DNS as a TXT record at selector._domainkey.yourdomain.com. The receiving server reads the s= (selector) and d= (domain) tags from the signature, fetches that public key, and verifies the hash. If it matches, the message is proven to be authorised by the domain and unaltered in the signed parts.
Reading a DKIM signature
This is a trimmed signature from a message sent through PostStack for example.com:
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com;
q=dns/txt; s=poststack; bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
h=from:subject:date:message-id:to:mime-version:content-type:list-unsubscribe;
b=Fv1kTqJ8...d=is the signing domain. For DMARC to pass on DKIM it must match, or be a parent of, the domain in the visibleFrom:.s=is the selector: the receiver looks uppoststack._domainkey.example.comto find the key.h=lists the headers covered by the signature. Anything not listed can be changed in transit without breaking it, which is whyList-Unsubscribebelongs in the list.c=relaxed/relaxedtolerates harmless whitespace and header-case changes made by relays;bh=is the body hash andb=the signature itself.
The matching DNS record is a single TXT value:
poststack._domainkey.example.com. TXT "v=DKIM1; k=rsa; h=sha256; p=MIIBIjANBgkqh..."A 2048-bit public key is longer than the 255-character limit of one TXT string, so most DNS providers split it into several quoted chunks. That is correct: receivers join the chunks back together before reading p=.
What DKIM protects against
DKIM defends against two things: spoofing (someone sending mail that claims to be from your domain) and tampering (a relay quietly changing the body or key headers in transit). It does not encrypt the message and it does not hide the contents — it is an integrity and authenticity check, not a confidentiality one.
Why DKIM fails
- The record is missing or truncated. A key pasted without its last characters, or with quotes inside the value, fails verification even though a record exists. Compare the published
p=with the key your provider shows, character for character. - The key in DNS is an old one. After a provider change or a rotation, the selector can still resolve to a key nobody signs with any more.
- Something rewrote the message after signing. Mailing lists that add a footer or change the subject break the body hash or a signed header. This is the main reason DMARC accepts either DKIM or SPF.
- The signature is valid but unaligned. A provider that signs with its own domain produces a DKIM pass that does nothing for DMARC on yours.
Key rotation
Because the selector is part of the DNS record name, a domain can publish several keys at once and rotate them without downtime: publish a new selector, start signing with it, then retire the old one once no in-flight mail relies on it. Rotating DKIM keys periodically limits the damage if a private key is ever exposed.
DKIM on PostStack
Every sending domain gets its own 2048-bit RSA key pair when you add it. The first selector is poststack; the private key never leaves PostStack, and only the public half goes into the record you publish. If the domain’s DNS is connected to HostStack, the record is written into your zone for you. Messages sent through the API are signed with rsa-sha256 and relaxed/relaxed canonicalisation, and the List-Unsubscribe header is inside the signature.
A rotation runs in three steps, and both keys stay valid throughout:
- Start.
POST /domains/:id/dkim/rotatecreates a new key under a dated selector such asps-20261109-a3f1and returns the record to publish. Mail is still signed with the current key. - Activate. PostStack looks up the new record on your domain’s authoritative nameservers and only switches when the published key matches exactly. It then waits 30 minutes for resolver caches to catch up, and switches signing to the new key. A background job checks every 15 minutes, so you do not have to call activate yourself.
- Retire. The old selector stays published for seven days so mail still in flight or sitting in a retry queue verifies, then it is removed.
import { PostStack } from '@poststack.dev/sdk';
const poststack = new PostStack(process.env.POSTSTACK_API_KEY!);
const { domains } = await poststack.domains.list();
const domain = domains.find((d) => d.name === 'example.com')!;
const rotation = await poststack.domains.startDkimRotation(domain.id);
console.log(rotation.pendingRecordName); // ps-20261109-a3f1._domainkey.example.com
console.log(rotation.pendingRecordValue); // v=DKIM1; k=rsa; h=sha256; p=...Each step fires a webhook (domain.dkim_rotation_started, domain.dkim_rotation_activated, domain.dkim_rotation_completed), so a rotation can be scripted end to end. Rotations are started by you; PostStack does not rotate keys on a timer.
Separately, PostStack re-reads the active key from your authoritative nameservers every six hours. If the record has disappeared or no longer matches the key it signs with — usually because someone edited the zone — the team gets a notification and the domain owner an email, before receivers start failing the signature.
Frequently asked questions
What does DKIM do?
DKIM adds a digital signature to the header of every outgoing email. The receiving mail server fetches the sender’s public key from DNS (at selector._domainkey.yourdomain.com) and uses it to verify the signature. A valid signature proves two things: the message genuinely came from a server authorised by the domain, and the signed parts of the message were not modified between sending and delivery.
What is a DKIM selector?
A selector is a label that lets a single domain publish more than one DKIM key at a time — useful for key rotation or for using different keys per sending service. It appears in the DNS record name as selector._domainkey.yourdomain.com and in the email’s DKIM-Signature header as the s= tag, so the receiver knows which key to fetch.
Is DKIM enough on its own?
No. DKIM proves a message is authentic and unaltered, but it does not tell receivers what to do with mail that fails. You pair DKIM with SPF (which authorises sending IPs) and DMARC (which sets the policy and alignment rules and turns on reporting). All three together are what mailbox providers like Gmail and Yahoo now effectively require for bulk senders.
How often should DKIM keys be rotated?
There is no fixed rule; many teams rotate once or twice a year, and immediately if a private key may have been exposed or a person or vendor with access to it leaves. What matters more than the interval is that rotation is routine rather than an emergency procedure. In PostStack a rotation publishes a new selector next to the old one, switches signing only once the new key is visible on your authoritative nameservers, and keeps the old selector for seven days, so it can be done at any time without failed signatures.
Should I use a 1024-bit or 2048-bit DKIM key?
2048-bit. 1024-bit RSA keys are still accepted by receivers but are considered weak, and some providers already score them lower. The only cost of 2048 bits is that the public key no longer fits in a single 255-character TXT string, so DNS splits it into chunks — which every receiver handles. PostStack generates 2048-bit keys for every domain.