1

在开发环境中,我测试了 Flask 邮件程序成功连接到个人 Gmail 帐户。我切换到具有相同配置结构的 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

配置文件

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'

路线.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)

电子邮件.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 地址进行了新登录。但是,从未发送过重置密码电子邮件。

我尝试了很多配置文件的调整,但问题可能出在其他地方。任何帮助将不胜感激。

更新:

在 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 上的一些封闭问题中得到了解决,但我无法让这段代码工作..

4

0 回答 0