Derek Stegelman

Django Dev Email

A few days ago I open sourced Django Dev Email, which is a simple replacement email backend for use in development environments. At K-State, when we deploy applications to a staging environment, we like to give clients access to the email account that all emails generated by the site will go to. We do this to avoid having emails sent to real email addresses during testing and development. For a while there were several nasty if statements throughout the code base that pointed emails at a different specified address. This technique sucks both for re-usabilty of code, and it messes with how email would be sent to the backend.

To resolve this, I wrote a simple backend that overrides the "to" address field and always replaces it with whatever value is in "DEV_EMAIL". When I was writing this, I could have inherited the base SMTP backend and just overridden the _send method, however, because it was marked private I ended up copying the entire class. Still not sure if that was the right move or not. Head over and take a look. As always pull requests and issues are welcome.

def _send(self, email_message):
"""A helper method that does the actual sending."""
if not email_message.recipients():
return False
from_email = sanitize_address(email_message.from_email, email_message.encoding)
# We are going to disregard the recipients, and force them to use the DEV_EMAIL setting.
try:
recipients = sanitize_address(settings.DEV_EMAIL, email_message.encoding)
except:
raise ImproperlyConfigured("You must set a DEV_EMAIL setting to use the Development Email Backend.")
try:
self.connection.sendmail(from_email, recipients,
    email_message.message().as_string())
    except:
    if not self.fail_silently:
    raise
    return False
    return True
    ```
    Django Dev Email on Github