Django Send HTML Emails

You can use this little function to load emails from a template and send them in both HTML and plaintext formats.

from django.core.mail import EmailMultiAlternatives
from django.template import loader, Context
from django.conf import settings

def send_multipart_email(subject, template, data_dict, recipient_list, from_email=settings.DEFAULT_FROM_EMAIL):
    if type(recipient_list) != list: recipient_list = [recipient_list]
    tt = loader.get_template(template+'.txt')
    c = Context(data_dict)
    e = EmailMultiAlternatives(subject, tt.render(c), from_email, recipient_list)
        ht = loader.get_template(template+'.html')
        e.attach_alternative(ht.render(c), 'text/html')

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *