Created
August 20, 2011 05:09
-
-
Save joestump/1158695 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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