当前位置:   article > 正文

(学习flask) 05 使用flask-mail_flask-mail 使用 163 邮箱

flask-mail 使用 163 邮箱

使用flask-mail扩展

pip install flask-mail
  • 1

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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.初始化邮箱

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

from flask_mail import Mail,Message
mail=Mail(app)
  • 1
  • 2

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>'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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>'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/857220
推荐阅读
相关标签
  

闽ICP备14008679号