码迷,mamicode.com
首页 > 其他好文 > 详细

Flask异步发送邮件

时间:2015-07-10 18:21:51      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

from flask import Flask
from flask.ext.mail import Mail, Message
from threading import Thread

app = Flask(__name__)

app.config.update(dict(
    MAIL_SERVER=smtp.qq.com,
    MAIL_PORT=587,
    MAIL_USE_TLS=True,
    MAIL_USERNAME = xxx@qq.com, #use to send email
    MAIL_PASSWORD=yyy #password
))

mail = Mail(app)

def send_async_email(msg):
    with app.app_context():
        mail.send(msg)

def send_email():
    msg = Message(Hello, World!, #主题
   sender=app.config[MAIL_USERNAME],  #发件人
   recipients=[zzz@163.com] #收件人
    )
    msg.body = "Hello" #纯文本正文
    msg.html = "World" #富文本正文(测试时邮件显示这个内容)
    thr = Thread(target=send_async_email, args=[msg])
    thr.start()
    return thr   

@app.route(/)
def index():
    send_email()
    return <h1>Hello,World!</h1>

if __name__ == __mian__:
    app.run()

 

Flask异步发送邮件

标签:

原文地址:http://www.cnblogs.com/bukekangli/p/4636143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!