Login

send_mail wrapper with DEBUG email trapping

Author:
udfalkso
Posted:
February 20, 2010
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

By using this simple wrapper instead of Django's default send_mail function, you gain the peace of mind of knowing that when settings.DEBUG == True, all the emails will be sent to you instead of the original recipient. Handy for testing.

1
2
3
4
5
6
7
8
from django.core.mail import send_mail as django_send_mail
from django.conf import settings

def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None):
    if settings.DEBUG:
        message = "%s\n\n=========== DEBUG Email Intercepted =============\nOriginal Recipients: %s" % (message, ",".join(recipient_list))
        recipient_list = [settings.SERVER_EMAIL]
    django_send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None)

More like this

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

Comments

Please login first before commenting.