2

我正在使用 smtp.gmail.com 设置 Python Flask-Mail 配置,我正确地按照说明进行操作,但我不断收到 OSError [Errno 0]。

我正在使用适用于 Linux 的 Windows 子系统,我将 Python 2 和 Python 3 下载到 Linux 部分,并使用 Xming Server 生成 Sublime Text 来编写我的 Python 代码。

这是我的烧瓶邮件配置代码:

app.config.update(
        MAIL_SERVER = 'smtp.gmail.com',
        MAIL_PORT = 465,
        MAIL_USE_TLS = False,
        MAIL_USE_SSL = True,
        MAIL_USERNAME = os.environ.get('EMAIL_USER'),
        MAIL_PASSWORD = os.environ.get('EMAIL_PASS')
)

mail = Mail(app)

这是我的重置密码功能:

    @users.route("/reset_password", methods=['GET', 'POST'])
    def reset_request():
      if current_user.is_authenticated:
        return redirect(url_for('main.home'))
      form = RequestResetForm()
        if form.validate_on_submit():
           user = User.query.filter_by(email=form.email.data).first()
           send_reset_email(user)
           flash('An email has been sent with instructions to reset your password.', 'info')
           return redirect(url_for('users.login'))
        return render_template('reset_request.html', title='Reset Password', form=form)

      @users.route("/reset_password/<token>", methods=['GET', 'POST'])
      def reset_token(token):
        if current_user.is_authenticated:
          return redirect(url_for('main.home'))
        user = User.verify_reset_token(token)
          if user is None:
            flash('That is an invalid or expired token', 'warning')
            return redirect(url_for('users.reset_request'))
        form = ResetPasswordForm()
        if form.validate_on_submit():
          hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
          user.password = hashed_password
          db.session.commit()
          flash('Your password has been update! You are now able to log in', 'success')
          return redirect(url_for('users.login'))
        return render_template('reset_token.html', title="Reset Passowrd", form=form)

      def send_reset_email(user):
        token = user.get_reset_token()
        msg = Message('Password Reset Request', sender='noreply@demo.com', recipients=[user.email])
        msg.body = f'''To reset your password, visit the following link:
 {url_for('users.reset_token', token=token, _external=True)}

If you did not make this request then simply ignore this email and no changes will be made
'''
        mail.send(msg)

这是我得到的错误......

builtins.OSError OSError: [Errno 0] 错误

我正在尝试重置密码,当用户输入密码恢复表单时,它应该发送一封电子邮件,但它不起作用,而是我收到 OSError。先感谢您。

4

0 回答 0