1 from django.contrib.auth.models import User,Group
2 from django.core.mail import EmailMessage
3 from django.template import loader
4 from xerxes.tools.misc import make_iter
7 Notify someone about something.
9 def notify(recipients, template, context = {}):
10 recipients = make_iter(recipients)
11 # Keep a dict of address -> (firstname, lastname)
14 if (isinstance(r, User)):
15 to[r.email] = (r.first_name, r.last_name)
16 elif (isinstance(r, Group)):
17 to.update([(m.email, (m.first_name, m.last_name)) for m in r.user_set.all()])
19 # Assume it is an email address
22 for (address, name) in to.items():
26 context['first_name'] = name[0]
27 context['last_name'] = name[1]
29 rendered = loader.render_to_string(template, context)
30 (headers, body) = rendered.split('\n\n', 1)
32 # Turn the headers into a dict so EmailMessage can turn them into a
33 # string again. Bit pointless, but it works.
34 # Perhaps we should just use python email stuff directly. OTOH, we
35 # still always need to parse for the From header.
38 # If no From header is present, let EmailMessage do the default
42 for header in headers.split('\n'):
43 (field, value) = header.split(':')
46 elif (field == 'Subject'):
49 # Don't put From and Subject in the dict, else they'll be
51 headers_dict[field] = value
54 # Only setting the From address through headers won't set the
55 # envelope address right.
56 from_email = from_email,
59 to = [make_rfc822_recipient(address, name)],
60 headers = headers_dict
64 def make_rfc822_recipient(address, name):
66 Creates a rfc 822 style recipient: Firstname Lastname <address@domain.nl>
68 Takes an address and a tuple with firstname and lastname. The tuple can
69 also be None when no name is known.
72 return "%s %s <%s>" % (name[0], name[1], address)
76 # vim: set sts=4 sw=4 expandtab: