tornadomail 發送html模板文件

1、tornadomail介紹html

    tornadomail是一個用於tornado異步郵件發送的開源庫,代碼託管於github:https://github.com/equeny/tornadomail.git,github上面有使用說明和用例前端

2、實現發送模板郵件的理由python

    默認的郵件發送爲文本,文字排版差,若是是用於公司的郵件發送,好比發送邀請函,格調略低,通常公司的郵件發送都須要ui設計樣式,前端實現HTML模板,郵件發送時,發送HTML模板,替換其中的關鍵字後發送。git

3、發送模板郵件實現github

一、編寫模板文件web

<!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服務器

二、編寫tornado服務器程序app

#!/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.pydom

三、編寫發送郵件頁面

<!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

四、測試

一、運行服務器

wangande@wangande-MS-7808:~/python-workspace/web-site$ python testMailTemplate.py 
ERROR:root:start server port:8888

二、瀏覽器輸入地址,填寫發送信息

 

三、查看郵件

 

打完收工

相關文章
相關標籤/搜索