Skip to content

Instantly share code, notes, and snippets.

@sjl
Created September 8, 2011 15:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjl/1203640 to your computer and use it in GitHub Desktop.
Save sjl/1203640 to your computer and use it in GitHub Desktop.
Useful Django send_mail wrapper that will automatically add a plain text alternative to outgoing HTML emails.
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.defaultfilters import striptags
def send_mail(subject, html_message, from_email, recipient_list, fail_silently=False, connection=None):
text_message = striptags(html_message)
recipient_list = getattr(settings, 'EMAIL_RECIPIENTS_OVERRIDE', recipient_list)
msg = EmailMultiAlternatives(subject, text_message, from_email, recipient_list, connection=connection)
msg.attach_alternative(html_message, "text/html")
msg.send(fail_silently=fail_silently)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment