-
Notifications
You must be signed in to change notification settings - Fork 6
/
emailer.py
50 lines (35 loc) · 1.13 KB
/
emailer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import mandrill
import os
import logging
import jinja2
from totalimpactwebapp.testing import is_test_email
logger = logging.getLogger("ti.emailer")
def send(address, subject, template_name, context):
if is_test_email(address):
return False
templateLoader = jinja2.FileSystemLoader(searchpath="totalimpactwebapp/templates")
templateEnv = jinja2.Environment(loader=templateLoader)
html_template = templateEnv.get_template(template_name + ".html")
html_to_send = html_template.render(context)
mailer = mandrill.Mandrill(os.getenv("MANDRILL_APIKEY"))
addressee = {"email": address}
try:
addressee["name"] = context["name"]
except KeyError:
pass
msg = {
"html": html_to_send,
"subject": subject,
"from_email": "team@impactstory.org",
"from_name": "The Impactstory team",
"to": [addressee], # must be a list
"track_opens": True,
"track_clicks": True
}
try:
msg["tags"] = context["tags"]
except KeyError:
pass
mailer.messages.send(msg)
logger.info(u"Sent an email to " + address)
return msg