【问题标题】:Flask application smptlib sender refused error after heroku deploymentHeroku 部署后 Flask 应用程序 smptlib 发件人拒绝错误
【发布时间】:2021-09-04 04:13:02
【问题描述】:

message.send(连接) 2021-06-20T00:49:29.013482+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.9/site-packages/flask_mail.py”,第 427 行,在发送中 2021-06-20T00:49:29.013483+00:00 app[web.1]: connection.send(self) 文件 self.host.sendmail(sanitize_address(envelope_from or message.sender), 2021-06-20T00:49:29.013484+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.9/smtplib.py”,第 882 行,在 sendmail 2021-06-20T00:49:29.013484+00:00 app[web.1]: 提高 SMTPSenderRefused(code, resp, from_addr) 2021-06-20T00:49:29.013486+00:00 app[web.1]: smtplib.SMTPSenderRefused: (530, b'5.7.0 需要身份验证。在\n5.7.0 https://support.google.com/mail/?p=WantAuthError y24sm8273493qtn.57 - gsmtp 了解更多信息', 'xxx@gmail.com')

我已将我的烧瓶应用程序部署到 heroku,但我正面临此电子邮件身份验证错误。在我的本地机器上,电子邮件部分运行良好。只有在部署到 heroku 之后才会出现此错误。我检查了所有的电子邮件和密码变量,还允许不太安全的应用程序访问。有人可以请教。我正在使用烧瓶邮件

我的代码是

__init__.py
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('EMAIL_USER')
app.config['MAIL_PASSWORD'] = os.environ.get('EMAIL_PASS')

In routes.py
def send_reset_email(user):
    token=user.get_reset_token()
    msg=Message("Password  Reset Request",sender='xxx@gmail.com',recipients=[user.email])
    msg.body=f'''To reset your password visit the following link:
    {url_for('reset_password',token=token,_external=True)}
    If you did not make this request please ignore this message.
    '''
    mail.send(msg)
@app.route("/reset_request",methods=["GET","POST"])
def reset_request():
    form=ResetRequestForm()
    if form.validate_on_submit():
        user=User.query.filter_by(email=form.emailmain.data).first()
        if user is None:
            flash("No account exists with this email. Pls register first.",'danger')
        else:
            send_reset_email(user)
            flash("An email has been sent with instructions to reset your password",'success')
            time.sleep(2)
            return redirect(url_for('login'))

In models.py

def get_reset_token(self, expires_sec=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')


    @staticmethod
    def verify_reset_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None

谁能告诉我我错过了什么

【问题讨论】:

  • 首先,使用针对您的错误格式化的代码。其次它说需要身份验证,请查看您的收件箱,那里可能有谷歌安全邮件。当您尝试发送邮件时
  • 您是否在 Heroku 中添加了 EMAIL_USEREMAIL_PASS 作为环境变量?
  • 不知道怎么做?
  • @charchit 它在本地工作,所以我认为 gmail 没有问题,heroku 上可能缺少一些配置。那会不会有什么问题呢
  • @Ram 我刚刚将这些变量添加到heroku。现在它的说法是 smtplibb 身份验证错误,我是否必须将所有邮件变量添加到 heroku,无论是 python 代码中的哪一个:?

标签: python


【解决方案1】:

Google 不允许您以这种方式从 heroku 登录。您可以执行以下操作。

  1. 检查您的邮件,可能有一封告诉您登录您的帐户。
  2. 转到security 并启用启用不太安全的应用。

注意 - 这样做之后,我的应用程序可以正常工作,但有一天它再次给出了同样的错误。我从一个 SO 问题中得到了这个 DisplayUnlockCaptcha 链接。但是每当我发送电子邮件时,我都需要这样做。所以最后我得到了这个。

  1. 前往security启用两步验证并前往应用密码。
  2. 选择您的设备(我选择了 windows 计算机)并在应用程序中选择邮件。将生成应用密码。在 heroku 环境变量中用此密码替换您的密码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 2019-01-21
    • 2020-07-22
    • 2014-08-19
    • 2018-04-07
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多