经过一晚上的摸索整理,测试成功了用163发送邮件原书代码链接:https:github.comgreylihelloflaskblobmasterdemosemailapp.py作 经过一晚上的摸索整理, 测试成功了用163发送邮件 原书代码链接: https://gi
经过一晚上的摸索整理, 测试成功了用163发送邮件
原书代码链接: https://github.com/greyli/helloflask/blob/master/demos/email/app.py
作者博客首页: https://greyli.com/
相比于书中源码, 代码修改如下后运行成功:
import osfrom threading import Threadimport sendgridfrom sendgrid.helpers.mail import Email as SGEmail, Content, Mail as SGMailfrom flask_mail import Mail, Messagefrom flask_wtf import FlaskFormfrom wtforms import StringField, TextAreaField, SubmitFieldfrom wtforms.validators import DataRequired, Emailfrom flask import Flask, flash, redirect, url_for, render_template, requestapp = Flask(__name__)app.jinja_env.trim_blocks = Trueapp.jinja_env.lstrip_blocks = Trueapp.config.update( # 配置环境变量,可在本文件夹下运行python # >>>from app import app # >>>app.config # 查看配置项内容,字典格式 SECRET_KEY=os.getenv('SECRET_KEY', 'siec'), MAIL_SERVER=os.getenv('MAIL_SERVER', 'smtp.163.com'), MAIL_PORT=465, MAIL_USE_SSL=True, MAIL_USERNAME=os.getenv('MAIL_USERNAME', 'bob@163.com'), MAIL_PASSWORD=os.getenv('MAIL_PASSWORD', 'NOYZJJY'),#此处不输入密码,输入授权码 MAIL_DEFAULT_SENDER=('注册验证 '))mail = Mail(app)# send over SMTPdef send_smtp_mail(subject, to, body): message = Message(subject, recipients=[to], body=body) mail.send(message)附:
1.163中设置如下
2.sendgrid当前无法注册