Using Templates for Sending Emails

Using Templates for Sending Emails

Sending nice pretty emails en-mass, or at regular intervals requires work so they look good. Fortunately Django's templates offers a great way to email uniform looking emails to your users. This video shows you how to do it, and keep it simple.

19. Using Templates for Sending Emails
Jun 05, 2013 - 8 minutes
Download: mp4
comments powered by Disqus

resources main/views.py
from django.http import HttpResponse
from django.template import Context
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMessage

def email_one(request):
    subject = "I am a text email"
    to = ['buddy@buddylindsey.com']
    from_email = 'test@example.com'

    ctx = {
        'user': 'buddy',
        'purchase': 'Books'
    }

    message = render_to_string('main/email/email.txt', ctx)

    EmailMessage(subject, message, to=to, from_email=from_email).send()

    return HttpResponse('email_one')

def email_two(request):
    subject = "I am an HTML email"
    to = ['buddy@buddylindsey.com']
    from_email = 'test@example.com'

    ctx = {
        'user': 'buddy',
        'purchase': 'Books'
    }

    message = get_template('main/email/email.html').render(Context(ctx))
    msg = EmailMessage(subject, message, to=to, from_email=from_email)
    msg.content_subtype = 'html'
    msg.send()

    return HttpResponse('email_two')

Sending nice pretty emails en-mass, or at regular intervals requires work so they look good. Fortunately Django's templates offers a great way to email uniform looking emails to your users. This video shows you how to do it, and keep it simple.