Login

DKIM Email Backend

Author:
DrMeers
Posted:
April 21, 2010
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

Overrides the _send method of the default SMTP EmailBackend class to include a DKIM signature based on settings:

  1. DKIM_SELECTOR -- e.g. 'selector' if using selector._domainkey.example.com
  2. DKIM_DOMAIN -- e.g. 'example.com'
  3. DKIM_PRIVATE_KEY -- full private key string, including """-----BEGIN RSA PRIVATE KEY-----""", etc

You'll need pydkim.

Just include this code in project_name.email_backend.py, for example, and select it in your settings file; e.g. EMAIL_BACKEND = 'project_name.email_backend.DKIMBackend'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.core.mail.backends.smtp import EmailBackend
from django.conf import settings
import dkim # http://hewgill.com/pydkim

class DKIMBackend(EmailBackend):
    def _send(self, email_message):
        """A helper method that does the actual sending + DKIM signing."""
        if not email_message.recipients():
            return False
        try:
            message_string = email_message.message().as_string()
            signature = dkim.sign(message_string,
                                  settings.DKIM_SELECTOR,
                                  settings.DKIM_DOMAIN,
                                  settings.DKIM_PRIVATE_KEY)
            self.connection.sendmail(email_message.from_email,
                    email_message.recipients(),
                    signature+message_string)
        except:
            if not self.fail_silently:
                raise
            return False
        return True

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

mlissner (on April 25, 2010):

Do you know how this will work with other backends aside from SMTP?

#

lucaslain (on January 23, 2011):

DJango version that supports Email_Backend is 1.2 Any idea how to make this work on 1.1?

best!

#

Please login first before commenting.