在开发环境中,我测试了Flask mailer是否成功连接到个人Gmail帐户。我使用相同的config结构切换到Zoho,但是会产生以下错误:
>>> send_password_reset_email(user)
>>> Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "app/email.py", line 8, in send_async_email
mail.send(msg)
File "/home/user/.local/lib/python2.7/site-packages/flask_mail.py", line 492, in send
message.send(connection)
File "/home/user/.local/lib/python2.7/site-packages/flask_mail.py", line 152, in __exit__
self.host.quit()
File "/usr/lib/python2.7/smtplib.py", line 772, in quit
res = self.docmd("quit")
File "/usr/lib/python2.7/smtplib.py", line 393, in docmd
self.putcmd(cmd, args)
File "/usr/lib/python2.7/smtplib.py", line 341, in putcmd
self.send(str)
File "/usr/lib/python2.7/smtplib.py", line 333, in send
raise SMTPServerDisconnected('please run connect() first')
SMTPServerDisconnected: please run connect() first
config.py
class Config(object):
MAIL_SERVER = 'smtp.zoho.com'
MAIL_PORT = '587'
MAIL_USE_TLS = '1'
MAIL_DEFAULT_SENDER = 'admin@mydomain.com'
MAIL_USERNAME = 'admin@mydomain.com'
MAIL_PASSWORD = 'mypassword'
ADMINS = 'admin@mydomain.com'
routes.py
@app.route('/recover', methods=['GET', 'POST'])
def reset_password_request():
if current_user.is_authenticated:
return redirect(url_for('manage'))
form = ResetPasswordRequestForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user:
send_password_reset_email(user)
flash('Check your email for the instructions to reset your password')
return redirect(url_for('login'))
return render_template('recover.html', form=form)
email.py
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
msg.html = html_body
Thread(target=send_async_email, args=(app, msg)).start()
def send_password_reset_email(user):
with app.app_context(), app.test_request_context(base_url='https://example.com'):
token = user.get_reset_password_token()
send_email('[MyDomain] Reset Your Password',
sender=app.config['ADMINS'][0],
recipients=[user.email],
text_body=render_template('email/reset_password.txt', user=user, token=token),
html_body=render_template('email/reset_password.html', user=user, token=token))
Zoho帐户似乎配置正确(没有列入黑名单的IP等)。首次尝试使用此配置后,我收到了来自Zoho支持的电子邮件,提醒我新登录是使用新IP地址进行的。但是,从未发送过重置密码电子邮件。
我尝试了许多配置文件的调整,但问题可能出在其他地方。任何帮助将不胜感激。
更新:
将quit函数包装在try-except块中之后,我终于遇到了一个真正的错误,该错误导致无法发送电子邮件。完整的回溯现在看起来像:
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "app/custom_mailer.py", line 11, in send_async_email
mail.send(msg)
File "/home/user/.local/lib/python2.7/site-packages/flask_mail.py", line 495, in send
message.send(connection)
File "/home/user/.local/lib/python2.7/site-packages/flask_mail.py", line 430, in send
connection.send(self)
File "/home/user/.local/lib/python2.7/site-packages/flask_mail.py", line 195, in send
message.rcpt_options)
File "/usr/lib/python2.7/smtplib.py", line 750, in sendmail
self.rset()
File "/usr/lib/python2.7/smtplib.py", line 469, in rset
return self.docmd("rset")
File "/usr/lib/python2.7/smtplib.py", line 394, in docmd
return self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
尽管在Stackoverflow上一些未解决的问题中似乎可以解决此错误,但我无法使这段代码正常工作..