flask 发送邮件
最编程
2024-10-09 21:49:23
...
开通邮件IMAP/SMTP服务
以网易邮箱为例
点击开启发送验证后会收到一个密钥,记得保存好
编写代码
安装flask-mail
pip install flask-mail
在config.py文件中配置邮件信息
MAIL_SERVER:邮件服务器
MAIL_USE_SSL:使用SSL
MAIL_PORT:端口
MAIL_USERNAME:用户名
MAIL_PASSWORD:上面获得的密钥
MAIL_DEFAULT_SENDER:默认的发送者
#邮箱配置
MAIL_SERVER = "smtp.163.com"
MAIL_USE_SSL = True
MAIL_PORT = 465
MAIL_USERNAME = "188xxxx5@163.com"
MAIL_PASSWORD = "xxxxxxx"
MAIL_DEFAULT_SENDER = "18xxxxxxx@163.com"
在exts.py创建mail对象
# 这个文件的目的是为了解决循环引用
from flask_mail import Mail
mail = Mail()
在app.py初始化mail
from flask import Flask
import config
from exts import mail
from blueprints.auth import bp as auth_bp
app = Flask(__name__)
app.config.from_object(config)
mail.init_app(app)
app.register_blueprint(auth_bp)
if __name__ == '__main__':
app.run()
在蓝图下的auth.py文件中编写路由
subject:邮件标题
recipients:接收者
body:邮件内容
from flask import Blueprint
from exts import mail
from flask_mail import Message
bp = Blueprint("auth", __name__, url_prefix="/auth")
@bp.route("/mail/test")
def mail_test():
message = Message(subject="邮箱测试", recipients=["258xxxxxx9@qq.com"], body="这是一条测试邮件")
mail.send(message)
return "邮件发送成功"
测试
打开接收者的邮箱
项目地址
https://gitee.com/yohoo-just-play/liu_oa/
上一篇: OCR 模型研究和详细安装
下一篇: Git使用
推荐阅读
-
flask 发送邮件
-
实验 3 使用 Gpg4win 发送加密电子邮件
-
春笋计划 | 雷吉外卖 | 短信发送验证码功能到免费邮箱 发送验证码功能 | 代码实现
-
计算机毕业设计 基于 Flask + vue 的博客系统设计与实现 Python 毕业设计 Python 毕业设计题目 Flask 框架 Vue [含源代码 + 安装与调试]。
-
Postman】使用的接口测试工具-Postman 发送帖子请求
-
java 发送 mailemail 实践
-
Java 编程系列] 使用 Java 访问 Microsoft Graph 并实现发送电子邮件的功能。
-
python flask 实现了 mock 接口
-
浏览器在发送请求后关闭,服务器处理分析
-
Vue.js 和 Flask/Django 后端共同开发研究