首先在cofing里面配置
SECRET_KEY = 'cbfnb'
MAIL_SERVER = 'smtp.163.com'#电子邮件服务器的主机名或IP地址
MAIL_PORT = '25' #电子邮件服务器的端口
MAIL_USE_TLS = True#启用传输层安全
MAIL_USERNAME = 'qq3367812084@163.com'#邮件账户用户名
MAIL_PASSWORD = 'WVRCYIQOGJMYMPCI'#密码
在写一个方法
def send_mail(tiele,email,comment):
mail = Mail(app)
msg = Message(tiele,sender='qq3367812084@163.com',recipients=[email])
msg.html = comment
mail.send(msg)
底下的代码调用上面
@bp_yh.route('/zhuce', methods=['POST'])
def zhuce():
if request.method == 'POST':
data = get_data()
textemail = re.match('^[a-zA-z0-9]{4,20}@163\.com$', data['email'])
# try:
if textemail :
sql1 = """select * from users where email='%s'"""%data['email']
res = db1.find_one(sql1)
if res:
return jsonify({"code":1001,"mes":"用户已存在"})
else:
sql = """insert into users values(0,'%s','%s',%d,'%s',%d)"""%(data['name'],generate_password_hash(data['password']),0,data['email'],0)
db1.insert(sql)
send_mail('欢迎使用163邮箱',data['email'],"欢迎使用,点击以下<a href='http://localhost:5000/yh/validEmail?email="+data['email']+"'>链接</a>验证")
return jsonify({"code": 200})