使用flask mail发送mail

参考: http://pythonhosted.org/Flask-Mail/
使用pip安装插件:
pip install Flask-Mail
在flask程序的config.cfg中加入以下内容,根据你邮件客户端的配置或者邮箱服务商提供的帮助文档即可:
MAIL_SERVER = ‘mail.example.cn’
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = ‘example@niot.cn’
MAIL_DEFAULT_SENDER = ‘example@example.cn’
MAIL_PASSWORD = ‘mypwd’
mail.py代码如下:

from threading import Thread
from flask import current_app, render_template
from flask_mail import Mail, Message
g_mail = None
def send_async_email(app, msg):
    global g_mail
    with app.app_context():
        g_mail.send(msg)
def send_email(to, subject, html):
    global g_mail
    app = current_app._get_current_object()
    g_mail = Mail(app)
    msg = Message(subject=subject, recipients=to, html=html)
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()
    return thr

在需要发送邮件的文件,使用下面代码就可以异步发送邮件了

from mail import send_email
send_email(to, subject, html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值