-1

I am trying to send a reset password email using flask-mail. The code executes without errors but I didn't receive any email.

I tried the following:

  1. adding those line to the app.config in the init.py file:
app.config['MAIL_SUPPRESS_SEND'] = False
app.config['MAIL_DEBUG'] = True
app.config['DEBUG'] = True
app.config['MAIL_FAIL_SILENTLY'] = False 
app.config['TESTING'] = False
  1. Changing the (Less secure app access) setting in Gmail to be on.

Here are some lines of my code:

In init.py file:

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy 
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'

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')
app.config['MAIL_SUPPRESS_SEND'] = False
app.config['MAIL_DEBUG'] = True
app.config['DEBUG'] = True
app.config['MAIL_FAIL_SILENTLY'] = False 
app.config['TESTING'] = False
mail = Mail(app)

In routes.py file:

def send_reset_email(user):
    token = user.get_reset_token()
    msg = Message(subject='Password Reset Request', sender='noreply@demo.com', recipients=[user.email])

    msg.body = f''' To reset your password, visit the following link!:
{url_for('reset_token', token=token, _external=True)}

If you did not make this request then simply ignore this email and no changes will be made
'''

@app.route("/reset_password", methods=['GET', 'POST'])
def reset_request():
    if current_user.is_authenticated:
        return redirect(url_for('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('login'))
    return render_template('reset_request.html', title='Reset Password', form=form)

I expected to receive the reset email but I didn't and it fails silently.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

0

You will need to call mail.send(msg)

djnz
  • 2,021
  • 1
  • 13
  • 13
0

1) In add to init file

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

2) In route.py file

need for:

mail.send(msg)
Sankar guru
  • 935
  • 1
  • 7
  • 16