Guide to Enabling and Testing DKIM on Outgoing Email

If your emails are going to spam, DKIM is probably not being signed. Here you'll see key generation, record publishing, and real signature testing step by step.

7 min Updated 30 Nov 2026

You've sent the email, it reached Gmail, but a yellow banner sits atop the message: "Why is this message suspicious?" Or worse, it went straight to Spam. When you open the message headers, you see the Authentication-Results line and next to it says dkim=none. That means there's no signature at all. The problem isn't SMTP, and it isn't DNS alone either; you haven't yet enabled DKIM on the domain, or you've enabled it and haven't published the key correctly.

This article follows exactly that path: from key generation to seeing dkim=pass on a real email. If you're still not sure which server your emails are going out from, first check the domain with the MX record lookup tool; DKIM only makes sense on a domain that actually sends email.

First things first: what DKIM signs

DKIM adds a header to the message called DKIM-Signature that contains a hash of specific parts of the message (headers and body) and is signed with the server's private key. The recipient reads the public key from DNS and reconstructs the hash. If they match, pass. If the message was altered along the way, fail.

A point many people understand too late: DKIM prevents sender forgery, but on its own it doesn't determine that an email is "authorized." That's the job of SPF and DMARC. DKIM and SPF are two wings of the same bird, and DMARC is the umbrella over both. If you only set up DKIM and don't have SPF, messages may still be rejected.

Key generation: key length and choosing a selector

On the server, we generate a key with OpenSSL. Today's standard is 2048 bits; 1024 bits still works but some recipients give a negative score and some services won't accept it at all.

openssl genrsa -out dkim-private.pem 2048
openssl rsa -in dkim-private.pem -pubout -out dkim-public.pem

Now you need to place the public key as a TXT record in DNS. Take the contents of the dkim-public.pem file, remove the -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- lines, and join the rest onto a single line. You choose the selector yourself; for example mail or s1. The record name becomes mail._domainkey.example.com and its value:

v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...

That p= is the public key itself. If a single character is dropped or an extra line remains, the signature fails and you won't see a DNS error in the server log; only the recipient will say dkim=permerror. Remember the difference between fail and permerror: permerror means I couldn't read the record, fail means I read it but the hash didn't match.

Where to keep the key

Place the private key in a path readable by the email service, not in /root with 644 permissions. On Postfix it's usually /etc/opendkim/keys/example.com/mail.private and its owner should be opendkim with 600 permissions. If permissions are wrong, the service comes up but doesn't sign, and you'll see key load failed in the log.

Connecting to the email service: OpenDKIM on Postfix

If you run your own email server, OpenDKIM is the most common option. In /etc/opendkim.conf have these lines:

Domain                  example.com
Selector                mail
KeyFile                 /etc/opendkim/keys/example.com/mail.private
Socket                  inet:12301@localhost
Canonicalization        relaxed/relaxed
Mode                    sv

Mode sv means sign and verify. Then in /etc/postfix/main.cf:

milter_default_action = accept
milter_protocol = 6
smtpd_milters = inet:localhost:12301
non_smtpd_milters = inet:localhost:12301

And in /etc/default/opendkim align the socket with the same port. Then systemctl restart opendkim postfix. If you have a hosting panel, these tasks are usually done through the user interface and you only need to add the DNS record manually; the documentation and knowledge base explains the path for different panels.

Verification: how to tell the signature really works

First check the record from the outside. On your local terminal, not the server:

dig +short mail._domainkey.example.com TXT

You should see the same v=DKIM1; k=rsa; p=... string. If it's empty, the record isn't published or its name is wrong. Then send a real email to a Gmail address and in Gmail click "Show original." Look for this line:

dkim=pass header.i=@example.com header.s=mail header.b=...

If you see dkim=pass, you're done. If you see none, the service isn't signing (key not loaded or milter not connected). If you see fail, the DNS record doesn't match the private key. If you see permerror, the TXT record has a structural problem; the most common is the key being split across multiple TXT strings, which some DNS panels do on their own and must be manually converted to a single string.

This is where people go wrong

A common mistake I've seen many times: the site admin sets up DKIM on the main domain, but emails are sent with From: no-reply@mail.example.com or through an external transactional service. The result is that the signature is on the example.com domain but the message comes from a subdomain and the recipient can't find the selector. The sign is simple too: in the header you see header.d=example.com but dkim=none for the domain that actually sent it. The fix: either align the From with the signed domain, or set up a separate key and record for the subdomain too.

Second mistake: assuming that because DKIM passes, the email will definitely reach the Inbox. That's not the case. DKIM only says the message wasn't tampered with along the way. If the server IP is on a blacklist or the sending rate is high, that same message will go to Spam even with dkim=pass. For high-volume emails like passwords and invoices, it's better to use a transactional service; in the transactional email guide I've explained why sending them from shared hosting is a bad idea.

The cost this work carries

DKIM is free but not costless. Any change to the message after signing breaks the signature. If a server along your email's path adds a header or rewrites the body (for example some mailing lists or antivirus filters), the signature fails. That's why I recommend relaxed/relaxed rather than simple/simple: relaxed mode tolerates minor changes in spacing and header order and in practice gives a higher pass rate.

The second cost is key rotation. Change the 2048-bit key every year or two. If the key leaks, anyone can sign emails in your name. For rotation without downtime, create a new selector (for example s2), publish the record, wait a few hours for DNS to settle in caches, then move the service to the new selector and remove the old one.

When you're on shared hosting

On shared hosting you usually don't see the private key and the email service signs on its own. What you can do is only publish the TXT record; get its value from the panel or from support. If the panel has an "Enable DKIM" option, after enabling it always check with dig that the record is actually published, because some panels only generate the key and don't write the record to DNS. In the ServerNet blog I've written more examples of these cases.

If you want full control of DNS and the email server, Linux hosting lets you manage the records and the service yourself. But before any decision, first see whether the problem is really DKIM or something else; often the problem is the wrong SMTP port. In the email ports and encryption guide I've explained the difference between 587, 465, and 25 with examples.

Frequently asked questions

Why do I still see dkim=none even though I've set up the DKIM record?

It means your email service isn't signing at all. The DNS record only publishes the public key; the signature must be done on the sending server side. First make sure OpenDKIM or its equivalent is running and the milter is connected in Postfix. If you're on shared hosting, the DKIM option in the panel is probably off or the email service is configured for a different domain.

Does DKIM alone prevent email from going to spam?

No. DKIM is one of the three pillars of email authentication, alongside SPF and DMARC. Without SPF and DMARC, many recipients remain suspicious of your message. In addition, IP and domain reputation, sending rate, and message content all factor into the spam decision. DKIM is a necessary condition, not a sufficient one.

How long should the DKIM key be, 1024 or 2048 bits?

Choose 2048 bits. 1024 bits technically works but some large email services give a negative score to messages with shorter keys and in some cases ignore the signature. The only exception is where your DNS provider has a TXT string length limit; in that case either change the provider or carefully revert the key to 1024.

How can I tell the DKIM signature on a real email is correct?

Send an email to your own Gmail address, open the message, and click "Show original." In the Authentication-Results section you should see dkim=pass with header.d equal to your domain. If it's not pass, the error type (none, fail, or permerror) tells you exactly which stage the problem is at.

Next step: right now, send a test email to Gmail and open its header. If it's not dkim=pass, first check the TXT record with dig, then the signing service log. In ninety percent of cases, the problem is in that one record line.

Was this page helpful?