Python modules you should know: pydkim

May 08, 2012 at 04:40 PM | categories: Python, PyMYSK, Howto | View Comments

Next in our series of Python modules you should know is pydkim. This package is used to sign and verify email according to the DKIM standards from within Python programs.

Home page


The pydkim module is a Python module that implements DKIM (DomainKeys Identified Mail) email signing and verification.

DomainKeys Identified Mail (DKIM) lets an organization take responsibility for a message that is in transit. Technically DKIM provides a method for validating a domain name identity that is associated with a message through cryptographic authentication.

Although DKIM is implemented in most MTA's, in some cases you have a setup where you do not want to run and manage an SMTP server yourself say for example if your Python application uses Amazon SES to send out email. So you can use pydkim within your email generation code to sign your messages before passing them to SES.


pip install pydkim


The package provides a command line interface as well as a Python Class.

Python Class

Sign a message

from email.message import Message

from dkim import sign

body = """Hi There,

This is a simple message that will be signed by pydkim

--The signer
# compose a simple message
msg = Message()
msg['From'] = 'Sender <>'
msg['To'] = 'Recipient <>'
msg['Subject'] = 'Test message'

# sign the message
private_key = open('default.pem').read()
headers = ['To', 'From', 'Subject']
email = msg.as_string()
sig = sign(email, 'default', '', private_key, include_headers=headers)
print sig, email


DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple;;; q=dns/txt; s=default;
 t=1336488625; h=From : To : Subject;
 bh=skd2vA1wrCEzamCzKOvVKCaVhhpP1ihoQgGldgTSVUE=; b=vGQgSjJxkTZe+NZZl
From: Sender <>
To: Recipient <>
Subject: Test message

Hi There,

This is a simple message that will be signed by pydkim

--The signer

Verify a message (I use the same message signed above)

from dkim import verify

if verify(open('email.txt').read()):
    print "Email verified successfully"

Command line

Sign a message selector domain privatekeyfile [identity]

Verify a message

cat email.txt |
echo $?

And there is more.

For details of the API please refer to the documentation. Happy DKIMing

blog comments powered by Disqus