flask-mail(qq邮箱)

from flask_mail import Mail,Message
app.config['MAIL_SERVER']='smtp.qq.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
#此处应是QQ邮箱的授权码 app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]' app.config['FLASKY_MAIL_SENDER'] = 'Flasky Admin <1215475440@qq.com>' app.config['FLASKY_ADMIN'] = '1215475440@qq.com'

  发送

@app.route('/',methods=['GET','POST'])
def index():
msg = Message(subject="helloworld", sender='1215475440@qq.com', recipients=['1808863623@qq.com'])
msg.html = "testinghtml"
mail.send(msg)

 异步发送邮件

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

def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
                  sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    thr = Thread(target=send_async_email,args=[app,msg])
    thr.start()
    return thr

  

原文地址:https://www.cnblogs.com/liushaocong/p/7417992.html