赞
踩
pip install flask_mail
- app.config['MAIL_SERVER'] = 'smtp.qq.com'
- app.config['MAIL_PORT'] = 587
- app.config['MAIL_USE_TLS'] = True
- app.config['MAIL_USERNAME'] = 'xxx@qq.com' //邮箱账号
- app.config['MAIL_PASSWORD'] = 'xxxx' //QQ邮箱授权码
- mail = Mail(app)
配置项 | 默认值 | 功能 |
MAIL_SERVER | localhost | 邮箱服务器 |
MAIL_PORT | 25 | 端口 |
MAIL_USE_TLS | False | 是否使用TLS |
MAIL_USE_SSL | False | 是否使用SSL |
MAIL_DEBUG | app.debug | 是否为DEBUG模式,打印调试消息 |
MAIL_SUPPRESS_SEND | app.testing | 设置是否真的发送邮件,True不发送 |
MAIL_USERNAME | None | 用户名,填邮箱 |
MAIL_PASSWORD | None | 密码,填授权码 |
MAIL_DEFAULT_SENDER | None | 默认发送者,填邮箱 |
MAIL_MAX_EMAILS | None | 一次连接中的发送邮件的上限 |
MAIL_ASCII_ATTACHMENTS | False | 如果 MAIL_ASCII_ATTACHMENTS 设置成 True 的话,文件名将会转换成 ASCII 的。一般用于添加附件。 |
- # -*- coding: utf-8 -*-
- from flask import Flask, request
- from flask_script import Manager, Shell
- from flask_mail import Mail, Message
- from threading import Thread
- import os
-
- app = Flask(__name__)
- app.config['MAIL_SERVER'] = 'smtp.qq.com'
- app.config['MAIL_PORT'] = 587
- app.config['MAIL_USE_TLS'] = True
- app.config['MAIL_USERNAME'] = '57812xxx@qq.com'
- app.config['MAIL_PASSWORD'] = 'agulvzfyjlbehi'
- mail = Mail(app)
-
- msg = Message('标题', sender='xxxx@qq.com', recipients=['xxxxx@163.com'])
- msg.body = '内容'
- with app.app_context():
- mail.send(msg)
-
- if __name__ == '__main__':
- app.run()
- def send_async_email(app, msg):
- with app.app_context():
- mail.send(msg)
-
- @app.route('/send_email')
- def send_email():
- msg.body = '内容'
- thread = Thread(target=send_async_email, args=[app, msg])
- thread.start()
- return 'success'
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。