163邮箱发送
config
class Config:
#DEBUG = True
#JSON_AS_ASCII = False
# 短信
SECRET_KEY = 'ssfwer32'
MAIL_SERVER = 'smtp.163.com' # 电子邮件服务器的主机名或IP地址
MAIL_PORT = '25' # 电子邮件服务器的端口
MAIL_USE_TLS = True # 启用传输层安全
MAIL_USERNAME = '邮箱'
MAIL_PASSWORD = ''"imap申请码"
—main
写在注册蓝图下
from flask_mail import Mail, Message
mail = Mail(app)
@app.route(‘/testmail1’,methods=[“GET”])
def testmail1():
msg = Message(‘欢迎注册在线教育平台’, sender=‘guxingyu020410@163.com’, recipients=[‘guxingyu020410@163.com’]) # 发件人,收件人
msg.html = “”
mail.send(msg)
return “ok”
二、使用步骤
1.配置congig
代码如下(示例):
class Config:
#DEBUG = True
#JSON_AS_ASCII = False
# 短信
SECRET_KEY = 'ssfwer32'
MAIL_SERVER = 'smtp.163.com' # 电子邮件服务器的主机名或IP地址
MAIL_PORT = '25' # 电子邮件服务器的端口
MAIL_USE_TLS = True # 启用传输层安全
MAIL_USERNAME = '邮箱'
MAIL_PASSWORD = "imap申请码"
2.主端口配置main.py
代码如下(示例):
from flask import Flask# 配置跨域from flask_cors import CORSfrom utils.config import Configapp = Flask(__name__)CORS(app)app.config.from_object(Config)if __name__ == '__main__': app.run()
蓝图定义方法·使用
from flask_mail import Mail, Message
from flask import current_app as app
def send_mail(title,content,emaillist):
mail = Mail(app)
msg = Message(title, sender='guxingyu020410@163.com', recipients=emaillist) # sender发件人,recipients收件人
msg.html = content
mail.send(msg)
@user.route("/updatepasword")
def updatepasword():
send_mail("欢迎使用","dfghhj",['guxingyu020410@163.com'])
return "ok"