How to Fix Postfix DKIM Signing Error
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 2048to genkey) - Rotate DKIM keys annually
- Monitor DKIM verification via DMARC reports
- Add multiple selectors for zero-downtime key rotation
Common Mistakes with dkim sign
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro