之前的功能参考文章 https://blog.csdn.net/scwMason/article/details/86764431
#send_email.py
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.utils import parseaddr,formataddr
import smtplib
from_addr="××××" #你邮箱的地址
password="×××××" #登录密码
smtplib_server='smtp.sina.com' #smpt服务商类型
msg=MIMEMultipart()
msg['From']=Header(from_addr)
msg['Subject']=Header('Bear Home Regist Information')
将我们的注册视图函数更改为
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
form=RegistrationForm()
if form.validate_on_submit():
user=User(nickname=form.nickname.data,email=form.email.data,password=form.password.data)
content=MIMEText(
"Your Bear Home username is %s,password is %s"%(form.nickname.data,form.password.data),
'plain',
'utf-8'
)
msg.attach(content)
try:
server=smtplib.SMTP()
server.connect(smtplib_server,25)
server.set_debuglevel(1)
server.login(from_addr,password)
server.sendmail(from_addr,form.email.data,msg.as_string())
print("Successfully send")
except smtlib.SMTPException as e:
server.quit()
print('发送失败')
pass
db.session.add(user)
db.session.commit()
flash('Congratulations, you are now a registered user!')
return redirect(url_for('login'))
return render_template('register.html',title="Register",form=form)