一、tornadomail介绍
tornadomail是一个用于tornado异步邮件发送的开源库,代码托管于github:https://github.com/equeny/tornadomail.git,github上面有使用说明和用例
二、实现发送模板邮件的理由
默认的邮件发送为文本,文字排版差,如果是用于公司的邮件发送,比如发送邀请函,格调略低,一般公司的邮件发送都需要ui设计样式,前端实现HTML模板,邮件发送时,发送HTML模板,替换其中的关键字后发送。
三、发送模板邮件实现
1、编写模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="main_container" style="background-color: #e4e9f0;border: 1px solid #dcdfe3;padding: 40px;font-size:12px">
<div class="main" style="border-radius: 4px;padding-left: 20px;width:800px;margin: 0 auto;position: relative;box-shadow: 1px 1px 15px 1px rgba(0,0,0,.1);background-color: white;">
<div class="left_style" style="position: absolute;left: -16px;width: 20px;top: 10px;bottom: 7px;background-color:#e4e9f0;background-image:url('http://7xsmqt.com2.z0.glb.qiniucdn.com/left1.png')"></div>
<div class="main_content">
<div class="nav" style="height: 40px;border-bottom: 2px solid #F8B537;"></div>
<div class="content" style="padding: 30px 60px;text-align: left;">
<div class="title" style="padding-bottom: 20px;font-size: 14px;text-align: center;">{{ title }}</div>
<div class="content_detail">
<div class="user_info"><span>亲爱的</span><span style='margin:0 5px;'>{{ user_name }},</span><span>您好:</span></div>
<div class="link">
<div style="margin: 7px 0;">{{ content }}</div>
</div>
</div>
<div class="idealsee_info" style="margin: 30px 0;">
<div style="margin: 7px 0;">Tornado Mail Test</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
其中{{}}表示需要替换的关键字,和tornado的模板替换一样**
保存文件名:mail_template.html**
2、编写tornado服务器程序
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado import template
from tornado.options import define, options
from tornadomail.message import EmailFromTemplate # 导入EmailFromTemplate
from tornadomail.backends.smtp import EmailBackend
define("port", default=8888, help="run on the given port", type=int)
logging.basicConfig(level=logging.DEBUG)
class Application(tornado.web.Application):
@property
def mail_connection(self):
return EmailBackend(
"smtp.qiye.163.com",
25,
"send@163.cn", # 发送方邮箱(替换成自己的)
"password", # 发送方邮箱密码(替换成自己的)
True,
template_loader=template.Loader('.'), # 模板的加载路径
)
class MainHandler(tornado.web.RequestHandler):
@property
def mail_connection(self):
return self.application.mail_connection
def get(self):
self.render("index.html")
def post(self):
def _finish(num):
logging.error('sended %d message(s)' % num)
subject_cn = self.get_argument('subject', "") # 邮件标题
email = self.get_argument('email', "") # 收件人邮件地址
message = self.get_argument("message", "") # 邮件内容
msg_params = { # 模板需要替换的关键字参数
"title": subject_cn,
"user_name": email,
"content": message,
}
message = EmailFromTemplate(
subject=subject_cn, # 邮件标题
template="mail_template.html", # 模板文件,必须在template.Loader('.')下
params=msg_params, # 模板需要替换的关键字参数
from_email="send@163.cn", # 发送方邮件(替换成自己的)
to=[email], # 收件人邮件地址列表
connection=self.mail_connection
)
message.send(callback=_finish)
self.render("index.html")
def main():
# options.log_to_stderr = True
options.logging = 'info'
tornado.options.parse_command_line()
application = Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
logging.error('start server port:%s' % options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
保存文件名:testMailTemplate.py**
3、编写发送邮件页面
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Write e-mail</title>
</head>
<body>
<div id="body">
<form action="." method="post" accept-charset="utf-8">
<p><input placeholder="email" type="text" name="email" value=""></p>
<p><input placeholder="subject" type="text" name="subject" value=""></p>
<p><textarea placeholder="message" name="message" rows="8" cols="40"></textarea></p>
<p><input type="submit" value="Send"></p>
</form>
</div>
</body>
</html>
保存文件名:index.html
4、测试
1、运行服务器
wangande@wangande-MS-7808:~/python-workspace/web-site$ python testMailTemplate.py
ERROR:root:start server port:8888
2、浏览器输入地址,填写发送信息
3、查看邮件
网友评论