Skip to content

How to Fix Postfix DKIM Signing Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Postfix DKIM Signing Error. We cover key concepts, practical examples, and best practices.

Postfix outgoing emails are not DKIM-signed or the DKIM signature is invalid — the OpenDKIM filter is not configured or the milter socket connection fails.

The Problem

$ dig txt default._domainkey.example.com
# No DKIM record found, or signature shows "dkim=fail"

Step-by-Step Fix

Step 1: Install OpenDKIM

sudo apt-get install opendkim opendkim-tools

Step 2: Generate DKIM key

sudo opendkim-genkey -D /etc/dkimkeys/ -d example.com -s default
sudo chown opendkim:opendkim /etc/dkimkeys/default.private
sudo chmod 600 /etc/dkimkeys/default.private

Step 3: Configure OpenDKIM

# /etc/opendkim.conf
Domain                  example.com
KeyFile                 /etc/dkimkeys/default.private
Selector                default
Socket                  inet:8891@localhost

Step 4: Configure Postfix milter

# /etc/postfix/main.cf
milter_protocol = 2
milter_default_action = accept
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891

Step 5: Publish DNS record

cat /etc/dkimkeys/default.txt
default._domainkey IN TXT "v=DKIM1; h=sha256; k=rsa; p=MIGfMA0GCSqGSIb4DQEBAQUAA4GNADCBiQKBgQ..."

Step 6: Test DKIM signing

echo "Test DKIM" | mail -s "DKIM Test" recipient@example.com
sudo tail -f /var/log/mail.log | grep -i dkim

Expected: opendkim[1234]: 9A1B2C3D4E5F: DKIM-Signature placed

Prevention Tips

  • Use a 2048-bit key for better security (add -b 2048 to genkey)
  • Rotate DKIM keys annually
  • Monitor DKIM verification via DMARC reports
  • Add multiple selectors for zero-downtime key rotation

Common Mistakes with dkim sign

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world POSTFIX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does Postfix not add DKIM signatures?

The OpenDKIM milter is not connected. Check that opendkim is running and the Socket path matches. Test the milter connection: echo "test" | nc localhost 8891. Also verify Postfix has the correct smtpd_milters setting.

How do I verify a DKIM signature is correct?

Use opendkim-testmsg: opendkim-testmsg -d example.com -s default -D /etc/dkimkeys/ < email.txt. Or send a test email to a DKIM checking service like DKIMvalidator.com or mail-tester.com.

What DNS record is required for DKIM?

A TXT record at default._domainkey.example.com containing "v=DKIM1; h=sha256; k=rsa; p=<public-key>". The selector name (default) must match the KeyFile and Selector in opendkim.conf. Publish this record through your DNS provider.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro