Find file
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (46 sloc) 1.81 KB
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from django.conf import settings
from django.conf import EMAIL_TEMPLATE_ROOT
except ImportError:
EMAIL_TEMPLATE_ROOT = 'www/emails'
class Email(object):
Class for handling emailing in Django with built in templates.
A template's first line is used for the subject of the email.
Everything else makes up the body.
def __init__(self, template, to=None, data=None, from_name='Freshplum Mail'): = to
self.template = template = data or {}
self.from_name = from_name
def render(self):
data =
as_string = render_to_string('%s/%s.txt' % (EMAIL_TEMPLATE_ROOT, self.template),
lines = as_string.splitlines()
self.subject = lines[0]
self.body = "\n".join(lines[1:])
return self.subject, self.body
def _return_context_vars(self):
d = {'WEB_ROOT': ''}
if hasattr(, 'first_name'):
d['first_name'] =
return d
def get_from_address(self):
return '%s <%s>' % (self.from_name, settings.EMAIL_HOST_USER)
def send(self):
if not hasattr(self, 'subject'):
if type( == type([]):
to_field =
elif type( == type(''):
to_field = [to]
to_field = [str(getattr(, 'email'))]
except KeyError:
raise KeyError('Email address not found in to object') = EmailMessage(self.subject, self.body, from_email=self.get_from_address(), to=to_field)