forked from haldun/meetings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailsender.py
84 lines (63 loc) · 1.77 KB
/
mailsender.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import logging
import sys
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.header import Header
import hotqueue
import yaml
import tornado.options
import tornado.web
from tornado.options import define, options
define("config_file", default="app_config.yml", help="app_config file")
queue = hotqueue.HotQueue('mail', host='localhost', port=6379, db=0)
class Model(dict):
"""Like tornado.web._O but does not whine for non-existent attributes"""
def __getattr__(self, name):
try:
return self[name]
except KeyError:
return None
def __setattr__(self, name, value):
self[name] = value
class Mailer(object):
def __init__(self):
pass
@property
def config(self):
if not hasattr(self, '_config'):
logging.debug("Loading app config")
stream = file(options.config_file, 'r')
self._config = Model(yaml.load(stream))
return self._config
@queue.worker
def run(self, item):
try:
self.process_item(item)
except:
print sys.exc_info()
def process_item(self, item):
mail_user = self.config.mail_user
mail_pwd = self.config.mail_pwd
to = item['to']
subject = item['subject']
text = item['text']
msg = MIMEMultipart()
msg['From'] = mail_user
msg['To'] = to
msg['Subject'] = Header(subject, 'utf-8')
msg.attach(MIMEText(text.encode('utf-8')))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(mail_user, mail_pwd)
server.sendmail(mail_user, to, msg.as_string())
server.close()
logging.info("Sent invitation to %s" % to)
def main():
tornado.options.parse_command_line()
mailer = Mailer()
mailer.run()
if __name__ == '__main__':
main()