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)
13 if (isinstance(r, User)):
14 addresses.append(r.email)
15 elif (isinstance(r, Group)):
16 addresses += [m.email for m in r.user_set.all()]
18 # Assume it is an email address
20 # TODO: Make addresses unique
22 context['recipients'] = recipients
23 context['addresses'] = addresses
25 rendered = loader.render_to_string(template, context)
26 (headers, body) = rendered.split('\n\n', 1)
28 # Turn the headers into a dict so EmailMessage can turn them into a
29 # string again. Bit pointless, but it works.
30 # Perhaps we should just use python email stuff directly. OTOH, we
31 # still always need to parse for the From header.
34 # If no From header is present, let EmailMessage do the default
37 for header in headers.split('\n'):
38 (field, value) = header.split(':')
41 elif (field == 'Subject'):
44 # Don't put From and Subject in the dict, else they'll be
46 headers_dict[field] = value
49 # Only setting the From address through headers won't set the
50 # envelope address right.
51 from_email = from_email,
55 headers = headers_dict
58 # vim: set sts=4 sw=4 expandtab: