利用flask-mail实现邮件发送
qq邮箱版
from flask import Flask
from flask_mail import Mail,Message
from flask_script import Manager
app = Flask(__name__)
# 配置信息
# 电子邮件服务器地址
app.config["MAIL_SERVER"] = "smtp.qq.com"
# 设置邮箱端口为465,默认为25,由于阿里云禁止了25端口,所以需要修改
app.config["MAIL_PORT"] = 465
# qq邮箱需要开启SSL
app.config["MAIL_USE_SSL"] = True
# MAIL_USERNAME: 邮件账户的用户名
app.config["MAIL_USERNAME"] = "103252xxx@qq.com"
邮件账户的密码,为在qq中设置的授权码
app.config["MAIL_PASSWORD"] = "xxxxxxxxx"
mail = Mail(app)
manager = Manager(app=app)
@app.route('/')
def hello():
return '测试成功'
@app.route("/send_mail/")
def send_mail():
"""
发送邮件, sender为发送者邮箱, recipients为接受者邮箱
"""
message = Message("你好", sender="10325xxx@qq.com", recipients=["xxxxxxx@qq.com"])
message.body = "What are you doing"
send_email(message)
return "发送成功"
def send_email(msg):
with app.app_context():
mail.send(msg)
if __name__ == '__main__':
manager.run()
网易邮箱版
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
app.config["MAIL_SERVER"] = "smtp.163.com"
app.config["MAIL_PORT"] = 465 # 设置邮箱端口为465,默认为25,由于阿里云禁止了25端口,所以需要修改
app.config["MAIL_USE_SSL"] = True # 163邮箱需要开启SSL
app.config["MAIL_USERNAME"] = "ahahah@163.com"
app.config["MAIL_PASSWORD"] = "ahahahhahah"
mail = Mail(app)
@app.route("/send_mail")
def send_mail():
"""
发送邮件, sender为发送者邮箱, recipients为接受者邮箱
"""
message = Message("你好", sender=app.config["MAIL_USERNAME"], recipients=["1032524xxxx@qq.com"])
message.body = "what are you doing"
send_email(message)
return "发送成功"
def send_email(msg):
with app.app_context():
mail.send(msg)
if __name__ == "__main__":
app.run(port=8080)
网友评论