add a sinkhole email backend

It makes all mails sent by Django go to an unique recipient; use it on
development and preproduction platforms when there are productions
accounts loaded.
This commit is contained in:
Benjamin Dauvergne 2013-12-02 15:19:00 +01:00
parent 2eea590b6d
commit a17905116d
2 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,25 @@
from django.conf import settings
from django.core.mail.backends.smtp import EmailBackend as SmtpEmailBackend
from django.core.mail.message import sanitize_address
from django.utils.encoding import force_bytes
class EmailBackend(SmtpEmailBackend):
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)
recipients = [ settings.EMAIL_SINK_RECIPIENT ]
message = email_message.message()
charset = message.get_charset().get_output_charset() if message.get_charset() else 'utf-8'
try:
self.connection.sendmail(from_email, recipients,
force_bytes(message.as_string(), charset))
except:
if not self.fail_silently:
raise
return False
return True