-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_email.py
72 lines (58 loc) · 2.41 KB
/
send_email.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tornado.ioloop
import tornado.web
from tornado.options import options, define
import smtplib
from email.mime.text import MIMEText
# defines
define('port', type=int, default=8888)
define('host', type=str, default='localhost')
define('email_host', type=str)
define('email_port', type=int)
define('email_tls', type=bool, default=True)
define('email_username', type=str)
define('email_password', type=str)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Make post request to this URL to send mail.<br/>")
self.write("You should provide next field in POST request:<bt/>")
self.write("<ul><li>email_from</li><li>email_to</li><li>email_subject</li><li>email_body</li></ul><br/>")
self.write("Note that 'email_to' field may be passed few times in one request<br/><br/>")
self.write("""
<form method="POST">
<h6>Test form. Enter ddata here to check if email would be sent</h6>
Email from: <input name="email_from"/><br/>
Email to: <input name="email_to"/><br/>
Email Subject: <input name="email_subject"/><br/>
Email Body: <textarea name="email_body"></textarea><br/>
<button type="submit">Send!</button>
</form>
""")
def post(self):
connection = smtplib.SMTP(options.email_host,
options.email_port)
if options.email_tls:
connection.ehlo()
connection.starttls()
connection.ehlo()
if options.email_username and options.email_password:
connection.login(options.email_username, options.email_password)
msg = MIMEText(self.get_argument('email_body'))
msg['From'] = email_from = self.get_argument('email_from')
email_to = self.get_arguments('email_to')
msg['To'] = u', '.join(email_to)
msg['Subject'] = self.get_argument('email_subject')
connection.sendmail(email_from, email_to, msg.as_string())
connection.quit()
self.write("OK")
def main():
options.parse_command_line()
handlers = [
(r"/send_mail/", MainHandler),
]
application = tornado.web.Application(handlers)
application.listen(options.port, options.host)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()