栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python > python框架

Flask:邮件扩展

python框架 更新时间:发布时间: 百科书网 趣学号

邮件扩展

在开发过程中,很多应用程序都需要通过邮件提醒用户,Flask的扩展包Flask-Mail通过包装了Python内置的smtplib包,可以用在Flask程序中发送邮件。

Flask-Mail连接到简单邮件协议(Simple Mail Transfer Protocol,SMTP)服务器,并把邮件交给服务器发送。

设置邮箱授权码

如下示例,通过开启 QQ 邮箱验证 SMTP 服务设置,发送邮件:

#coding:utf-8
from flask import Flask,render_template
from flask_mail import Mail, Message
from threading import Thread
app = Flask(__name__)
# 配置邮件:服务器/端口/安全套接字层/邮箱名/授权码
app.config['MAIL_SERVER'] = "smtp.126.com"
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = "furuiyang@126.com"
app.config['MAIL_PASSWORD'] = "19940414"
app.config['MAIL_DEFAULT_SENDER'] = 'FlaskAdmin'
mail = Mail(app)
def async_send_email(app, msg):
    with app.app_context():
        try:
            mail.send(msg)
        except Exception as e:
            print e
def send_email_thread(subject, to, content):
    msg = Message(subject=subject, recipients=[to], body=content)
    thread = Thread(target=async_send_email,args=(app, msg))
    thread.start()
    return thread
@app.route('/')
def index():
    return '发送邮件' % url_for('send_email')
@app.route('/send_email')
def send_email():
    send_email_thread('我是邮件主题', to='furuiyang@126.com', content='我是邮件内容哈哈')
    return '发送中...'
if __name__ == '__main__':
    app.run()

众多python培训视频,尽在python学习网,欢迎在线学习!

本文转自:https://blog.csdn.net/Enjolras_fuu/article/details/82793428

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/12722.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号