Skip to content

Instantly share code, notes, and snippets.

@joestump
Created August 20, 2011 05:09
Show Gist options
  • Save joestump/1158695 to your computer and use it in GitHub Desktop.
Save joestump/1158695 to your computer and use it in GitHub Desktop.
class EmailMessage(EmailMultiAlternatives):
"""A small helper class that wraps the EmailMultiAlternatives class.
This allows you to pass a single template name, minus the extension, and
have it render each as a template. Text and HTML currently supported.
"""
TEMPLATES = ['txt', 'html']
def __init__(self, to, subject, template_name, **kwargs):
if not isinstance(to, list):
to = [to]
message = {}
for ext in EmailMessage.TEMPLATES:
t = loader.get_template('%s.%s' % (template_name, ext))
c = kwargs.get('context', {})
c['site'] = Site.objects.get_current()
message[ext] = t.render(Context(c))
super(EmailMessage, self).__init__(_(subject), message['txt'], None, to)
self.attach_alternative(message['html'], 'text/html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment