(学习flask) 05 使用flask-mail

使用flask-mail扩展

pip install flask-mail

1.配置邮箱

(1)我这里使用的是163邮箱,首先需要打开邮箱,找到下图这里
在这里插入图片描述
(2)开启SMTP服务,然后申请授权密码,需要绑定手机号
在这里插入图片描述
(3)163的服务器地址和端口如下
请添加图片描述

2.配置app.config

这里把用户名和密码都配进了本地的系统变量中

from flask import Flask
import os

app=Flask(__name__)
app.config['MAIL_SERVER']='smtp.163.com'
app.config['MAIL_PORT']=994
app.config['MAIL_USE_TLS']=False
app.config['MAIL_USE_SSL']=True
app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD')

3.初始化邮箱

flask_mail会自动帮我们创建好SMTP服务器

from flask_mail import Mail,Message
mail=Mail(app)

4.同步方式发送邮件

@app.route('/sendMail')
def sendMail():
    msg=Message('test message',sender=os.environ.get('MAIL_USERNAME'),recipients=['xxxxxx@qq.com'])
    msg.body='this is body!'
    msg.html='<b>HTML</b> body'
    with app.app_context():
        mail.send(msg)
    return '<h1>发送成功!</h1>'

5.异步方式发送邮件

def send_async_mail(app,msg):
    with app.app_context():
        mail.send(msg)

def send_mail(to,subject,template,**kwargs):
    msg=Message('async test '+subject,sender=os.environ.get('MAIL_USERNAME'),recipients=[to])
    msg.body=render_template(template+'.txt',**kwargs)
    msg.body = render_template(template+'.html', **kwargs)
    th=Thread(target=send_async_mail,args=[app,msg])
    th.start()
    return th

@app.route('/sendSync')
def send_async():
    to='xxxxxxxx@qq.com'
    subject='ASYNC'
    template='test'
    send_mail(to,subject,template)
    return '<h1>ASYNC 发送成功!</h1>'

6.完整代码

# coding:utf-8
from flask import Flask,render_template
import os
from flask_mail import Mail,Message

from threading import Thread

app=Flask(__name__)
app.config['MAIL_SERVER']='smtp.163.com'
app.config['MAIL_PORT']=994
app.config['MAIL_USE_TLS']=False
app.config['MAIL_USE_SSL']=True
app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD')
mail=Mail(app)

def send_async_mail(app,msg):
    with app.app_context():
        mail.send(msg)

def send_mail(to,subject,template,**kwargs):
    msg=Message('async test '+subject,sender=os.environ.get('MAIL_USERNAME'),recipients=[to])
    msg.body=render_template(template+'.txt',**kwargs)
    msg.body = render_template(template+'.html', **kwargs)
    th=Thread(target=send_async_mail,args=[app,msg])
    th.start()
    return th

@app.route('/sendSync')
def send_async():
    to='xxxxxxx@qq.com'
    subject='ASYNC'
    template='test'
    send_mail(to,subject,template)
    return '<h1>ASYNC 发送成功!</h1>'

@app.route('/sendMail')
def sendMail():
    msg=Message('test message',sender=os.environ.get('MAIL_USERNAME'),recipients=['xxxxxxx@qq.com'])
    msg.body='this is body!'
    msg.html='<b>HTML</b> body'
    with app.app_context():
        mail.send(msg)
    return '<h1>发送成功!</h1>'

if __name__=='__main__':
    app.run(debug=True)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值