2

I am setting up a Python Flask-Mail configuration using smtp.gmail.com, I followed the instructions correctly, but I keep getting the OSError [Errno 0].

I am using the Windows Subsystem for Linux, I downloaded both Python 2 and Python 3 into the Linux portion and using Xming Server to generate Sublime Text to write my Python code.

This is my code for flask-mail configuration:

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)

Here is my reset password functions:

    @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)

And this is the error I get...

builtins.OSError OSError: [Errno 0] Error

I'm trying to make a reset password, when the user enters the password recovery form, it should send an email, but it doesn't work and instead I get OSError. Thank you in advance.

JHero23
  • 149
  • 3
  • 10

0 Answers0