-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.py
50 lines (35 loc) · 1.63 KB
/
mail.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
49
50
import requests
from tornado.template import Loader
import db
import config
def send(to, subject, text=None, html=None):
url = 'https://api.mailgun.net/v2/%s/messages' % config.mail.url
data = {'from': config.mail.from_address,
'to': to,
'subject': subject}
if text:
data['text'] = text
if html:
data['html'] = html
return requests.post(url, auth=('api', config.mail.key), data=data)
def send_confirmation(user):
if user.unsubscribed:
return
loader = Loader('templates/mail')
text = loader.load('confirm.txt').generate(user=user, config=config.web)
html = loader.load('confirm.html').generate(user=user, config=config.web)
send(user.email, 'Confirmation instructions', text=text, html=html)
def send_alert(user_id, alert, alert_value):
user = db.get_email_attributes(user_id)
if user.unsubscribed:
return
alert.email_description = alert.email_description.replace('REPLACE_TRIGGER', str(alert.option_1_value))
alert.email_description = alert.email_description.replace('REPLACE_ACTUAL', str(alert_value))
if int(alert.interval) > 1440:
alert.interval = "%d day(s)" % (int(alert.interval)/1440) # minutes to days
else:
alert.interval = "%d hour(s)" % (int(alert.interval)/60) # minutes to hours
loader = Loader('templates/mail')
text = loader.load('alert.txt').generate(user=user, alert=alert, config=config.web)
html = loader.load('alert.html').generate(user=user, alert=alert, config=config.web)
send(user.email, 'Character alert: ' + alert.name , text=text, html=html)