Login

DKIM Email Backend

Author:
DrMeers
Posted:
April 21, 2010
Language:
Python
Version:
1.1
Tags:
email dkim
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. Command to dump data as a python script by willhardy 7 years ago
  2. Mobilize your Django site by stevena0 6 years, 2 months ago
  3. reCAPTCHA integration by marco.fucci 5 years, 11 months ago
  4. Unobtrusive comment moderation by ubernostrum 8 years, 3 months ago
  5. YUI Loader as Django middleware by akaihola 7 years, 2 months 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.