2018-02-06T15:11:28Z

The Flask Mega-Tutorial Part X: Email Support

This is the tenth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how your application can send emails to your users, and how to build a password recovery feature on top of the email support.

For your reference, below is a list of the articles in this series.

Note 1: If you are looking for the legacy version of this tutorial, it's here.

Note 2: If you would like to support my work on this blog, or just don't have patience to wait for weekly articles, I am offering the complete version of this tutorial packaged as an ebook or a set of videos. For more information, visit courses.miguelgrinberg.com.

The application is doing pretty well on the database front now, so in this chapter I want to depart from that topic and add another important piece that most web applications need, which is the sending of emails.

Why does an application need to email its users? There are many reasons, but one common one is to solve authentication related problems. In this chapter I'm going to add a password reset feature for users that forget their password. When a user requests a password reset, the application will send an email with a specially crafted link. The user then needs to click that link to have access to a form in which to set a new password.

The GitHub links for this chapter are: Browse, Zip, Diff.

Introduction to Flask-Mail

As far as the actual sending of emails, Flask has a popular extension called Flask-Mail that can make the task very easy. As always, this extension is installed with pip:

(venv) $ pip install flask-mail

The password reset links will have a secure token in them. To generate these tokens, I'm going to use JSON Web Tokens, which also have a popular Python package:

(venv) $ pip install pyjwt

The Flask-Mail extension is configured from the app.config object. Remember when in Chapter 7 I added the email configuration for sending yourself an email whenever an error occurred in production? I did not tell you this then, but my choice of configuration variables was modeled after Flask-Mail's requirements, so there isn't really any additional work that is needed, the configuration variables are already in the application.

Like most Flask extensions, you need to create an instance right after the Flask application is created. In this case this is an object of class Mail:

app/__init__.py: Flask-Mail instance.

# ...
from flask_mail import Mail

app = Flask(__name__)
# ...
mail = Mail(app)

If you are planning to test sending of emails you have the same two options I mentioned in Chapter 7. If you want to use an emulated email server, Python provides one that is very handy that you can start in a second terminal with the following command:

(venv) $ python -m smtpd -n -c DebuggingServer localhost:8025

To configure for this server you will need to set two environment variables:

(venv) $ export MAIL_SERVER=localhost
(venv) $ export MAIL_PORT=8025

If you prefer to have emails sent for real, you need to use a real email server. If you have one, then you just need to set the MAIL_SERVER, MAIL_PORT, MAIL_USE_TLS, MAIL_USERNAME and MAIL_PASSWORD environment variables for it. If you want a quick solution, you can use a Gmail account to send email, with the following settings:

(venv) $ export MAIL_SERVER=smtp.googlemail.com
(venv) $ export MAIL_PORT=587
(venv) $ export MAIL_USE_TLS=1
(venv) $ export MAIL_USERNAME=<your-gmail-username>
(venv) $ export MAIL_PASSWORD=<your-gmail-password>

If you are using Microsoft Windows, you need to replace export with set in each of the export statements above.

Remember that the security features in your Gmail account may prevent the application from sending emails through it unless you explicitly allow "less secure apps" access to your Gmail account. You can read about this here, and if you are concerned about the security of your account, you can create a secondary account that you configure just for testing emails, or you can enable less secure apps only temporarily to run your tests and then revert back to the more secure default.

Flask-Mail Usage

To learn how Flask-Mail works, I'll show you how to send an email from a Python shell. So fire up Python with flask shell, and then run the following commands:

>>> from flask_mail import Message
>>> from app import mail
>>> msg = Message('test subject', sender=app.config['ADMINS'][0],
... recipients=['your-email@example.com'])
>>> msg.body = 'text body'
>>> msg.html = '<h1>HTML body</h1>'
>>> mail.send(msg)

The snippet of code above will send an email to a list of email addresses that you put in the recipients argument. I put the sender as the first configured admin (I've added the ADMINS configuration variable in Chapter 7). The email will have plain text and HTML versions, so depending on how your email client is configured you may see one or the other.

So as you see, this is pretty simple. Now let's integrate emails into the application.

A Simple Email Framework

I will begin by writing a helper function that sends an email, which is basically a generic version of the shell exercise from the previous section. I will put this function in a new module called app/email.py:

app/email.py: Email sending wrapper function.

from flask_mail import Message
from app import mail

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
    mail.send(msg)

Flask-Mail supports some features that I'm not utilizing here such as Cc and Bcc lists. Be sure to check the Flask-Mail Documentation if you are interested in those options.

Requesting a Password Reset

As I mentioned above, I want users to have the option to request their password to be reset. For this purpose I'm going to add a link in the login page:

app/templates/login.html: Password reset link in login form.

    <p>
        Forgot Your Password?
        <a href="{{ url_for('reset_password_request') }}">Click to Reset It</a>
    </p>

When the user clicks the link, a new web form will appear that requests the user's email address as a way to initiate the password reset process. Here is the form class:

app/forms.py: Reset password request form.

class ResetPasswordRequestForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
    submit = SubmitField('Request Password Reset')

And here is the corresponding HTML template:

app/templates/reset_password_request.html: Reset password request template.

{% extends "base.html" %}

{% block content %}
    <h1>Reset Password</h1>
    <form action="" method="post">
        {{ form.hidden_tag() }}
        <p>
            {{ form.email.label }}<br>
            {{ form.email(size=64) }}<br>
            {% for error in form.email.errors %}
            <span style="color: red;">[{{ error }}]</span>
            {% endfor %}
        </p>
        <p>{{ form.submit() }}</p>
    </form>
{% endblock %}

I also need a view function to handle this form:

app/routes.py: Reset password request view function.

from app.forms import ResetPasswordRequestForm
from app.email import send_password_reset_email

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

This view function is fairly similar to others that process a form. I start by making sure the user is not logged in. If the user is logged in, then there is no point in using the password reset functionality, so I redirect to the index page.

When the form is submitted and valid, I look up the user by the email provided by the user in the form. If I find the user, I send a password reset email. I'm using a send_password_reset_email() helper function to do this. I will show you this function below.

After the email is sent, I flash a message directing the user to look for the email for further instructions, and then redirect back to the login page. You may notice that the flashed message is displayed even if the email provided by the user is unknown. This is so that clients cannot use this form to figure out if a given user is a member or not.

Password Reset Tokens

Before I implement the send_password_reset_email() function, I need to have a way to generate a password request link. This is going to be the link that is sent to the user via email. When the link is clicked, a page where a new password can be set is presented to the user. The tricky part of this plan is to make sure that only valid reset links can be used to reset an account's password.

The links are going to be provisioned with a token, and this token will be validated before allowing the password change, as proof that the user that requested the email has access to the email address on the account. A very popular token standard for this type of process is the JSON Web Token, or JWT. The nice thing about JWTs is that they are self contained. You can send a token to a user in an email, and when the user clicks the link that feeds the token back into the application, it can be verified on its own.

How do JWTs work? Nothing better than a quick Python shell session to understand them:

>>> import jwt
>>> token = jwt.encode({'a': 'b'}, 'my-secret', algorithm='HS256')
>>> token
b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhIjoiYiJ9.dvOo58OBDHiuSHD4uW88nfJikhYAXc_sfUHq1mDi4G0'
>>> jwt.decode(token, 'my-secret', algorithms=['HS256'])
{'a': 'b'}

The {'a': 'b'} dictionary is an example payload that is going to be written into the token. To make the token secure, a secret key needs to be provided to be used in creating a cryptographic signature. For this example I have used the string 'my-secret', but with the application I'm going to use the SECRET_KEY from the configuration. The algorithm argument specifies how the token is to be generated. The HS256 algorithm is the most widely used.

As you can see the resulting token is a long sequence of characters. But do not think that this is an encrypted token. The contents of the token, including the payload, can be decoded easily by anyone (don't believe me? Copy the above token and then enter it in the JWT debugger to see its contents). What makes the token secure is that the payload is signed. If somebody tried to forge or tamper with the payload in a token, then the signature would be invalidated, and to generate a new signature the secret key is needed. When a token is verified, the contents of the payload are decoded and returned back to the caller. If the token's signature was validated, then the payload can be trusted as authentic.

The payload that I'm going to use for the password reset tokens is going to have the format {'reset_password': user_id, 'exp': token_expiration}. The exp field is standard for JWTs and if present it indicates an expiration time for the token. If a token has a valid signature, but it is past its expiration timestamp, then it will also be considered invalid. For the password reset feature, I'm going to give these tokens 10 minutes of life.

When the user clicks on the emailed link, the token is going to be sent back to the application as part of the URL, and the first thing the view function that handles this URL will do is to verify it. If the signature is valid, then the user can be identified by the ID stored in the payload. Once the user's identity is known, the application can ask for a new password and set it on the user's account.

Since these tokens belong to users, I'm going to write the token generation and verification functions as methods in the User model:

app/models.py: Reset password token methods.

from time import time
import jwt
from app import app

class User(UserMixin, db.Model):
    # ...

    def get_reset_password_token(self, expires_in=600):
        return jwt.encode(
            {'reset_password': self.id, 'exp': time() + expires_in},
            app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        try:
            id = jwt.decode(token, app.config['SECRET_KEY'],
                            algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(id)

The get_reset_password_token() function generates a JWT token as a string. Note that the decode('utf-8') is necessary because the jwt.encode() function returns the token as a byte sequence, but in the application it is more convenient to have the token as a string.

The verify_reset_password_token() is a static method, which means that it can be invoked directly from the class. A static method is similar to a class method, with the only difference that static methods do not receive the class as a first argument. This method takes a token and attempts to decode it by invoking PyJWT's jwt.decode() function. If the token cannot be validated or is expired, an exception will be raised, and in that case I catch it to prevent the error, and then return None to the caller. If the token is valid, then the value of the reset_password key from the token's payload is the ID of the user, so I can load the user and return it.

Sending a Password Reset Email

Now that I have the tokens, I can generate the password reset emails. The send_password_reset_email() function relies on the send_email() function I wrote above.

app/email.py: Send password reset email function.

from flask import render_template
from app import app

# ...

def send_password_reset_email(user):
    token = user.get_reset_password_token()
    send_email('[Microblog] 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))

The interesting part in this function is that the text and HTML content for the emails is generated from templates using the familiar render_template() function. The templates receive the user and the token as arguments, so that a personalized email message can be generated. Here is the text template for the reset password email:

app/templates/email/reset_password.txt: Text for password reset email.

Dear {{ user.username }},

To reset your password click on the following link:

{{ url_for('reset_password', token=token, _external=True) }}

If you have not requested a password reset simply ignore this message.

Sincerely,

The Microblog Team

And here is the nicer HTML version of the same email:

app/templates/email/reset_password.html: HTML for password reset email.

<p>Dear {{ user.username }},</p>
<p>
    To reset your password
    <a href="{{ url_for('reset_password', token=token, _external=True) }}">
        click here
    </a>.
</p>
<p>Alternatively, you can paste the following link in your browser's address bar:</p>
<p>{{ url_for('reset_password', token=token, _external=True) }}</p>
<p>If you have not requested a password reset simply ignore this message.</p>
<p>Sincerely,</p>
<p>The Microblog Team</p>

The reset_password route that is referenced in the url_for() call in these two email templates does not exist yet, this will be added in the next section. The _external=True argument that I included in the url_for() calls in both templates is also new. The URLs that are generated by url_for() by default are relative URLs, so for example, the url_for('user', username='susan') call would return /user/susan. This is normally sufficient for links that are generated in web pages, because the web browser takes the remaining parts of the URL from the current page. When sending a URL by email however, that context does not exist, so fully qualified URLs need to be used. When _external=True is passed as an argument, complete URLs are generated, so the previous example would return http://localhost:5000/user/susan, or the appropriate URL when the application is deployed on a domain name.

Resetting a User Password

When the user clicks on the email link, a second route associated with this feature is triggered. Here is the password request view function:

app/routes.py: Password reset view function.

from app.forms import ResetPasswordForm

@app.route('/reset_password/<token>', methods=['GET', 'POST'])
def reset_password(token):
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    user = User.verify_reset_password_token(token)
    if not user:
        return redirect(url_for('index'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        user.set_password(form.password.data)
        db.session.commit()
        flash('Your password has been reset.')
        return redirect(url_for('login'))
    return render_template('reset_password.html', form=form)

In this view function I first make sure the user is not logged in, and then I determine who the user is by invoking the token verification method in the User class. This method returns the user if the token is valid, or None if not. If the token is invalid I redirect to the home page.

If the token is valid, then I present the user with a second form, in which the new password is requested. This form is processed in a way similar to previous forms, and as a result of a valid form submission, I invoke the set_password() method of User to change the password, and then redirect to the login page, where the user can now login.

Here is the ResetPasswordForm class:

app/forms.py: Password reset form.

class ResetPasswordForm(FlaskForm):
    password = PasswordField('Password', validators=[DataRequired()])
    password2 = PasswordField(
        'Repeat Password', validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Request Password Reset')

And here is the corresponding HTML template:

app/templates/reset_password.html: Password reset form template.

{% extends "base.html" %}

{% block content %}
    <h1>Reset Your Password</h1>
    <form action="" method="post">
        {{ form.hidden_tag() }}
        <p>
            {{ form.password.label }}<br>
            {{ form.password(size=32) }}<br>
            {% for error in form.password.errors %}
            <span style="color: red;">[{{ error }}]</span>
            {% endfor %}
        </p>
        <p>
            {{ form.password2.label }}<br>
            {{ form.password2(size=32) }}<br>
            {% for error in form.password2.errors %}
            <span style="color: red;">[{{ error }}]</span>
            {% endfor %}
        </p>
        <p>{{ form.submit() }}</p>
    </form>
{% endblock %}

The password reset feature is now complete, so make sure you try it.

Asynchronous Emails

If you are using the simulated email server that Python provides you may not have noticed this, but sending an email slows the application down considerably. All the interactions that need to happen when sending an email make the task slow, it usually takes a few seconds to get an email out, and maybe more if the email server of the addressee is slow, or if there are multiple addressees.

What I really want is for the send_email() function to be asynchronous. What does that mean? It means that when this function is called, the task of sending the email is scheduled to happen in the background, freeing the send_email() to return immediately so that the application can continue running concurrently with the email being sent.

Python has support for running asynchronous tasks, actually in more than one way. The threading and multiprocessing modules can both do this. Starting a background thread for email being sent is much less resource intensive than starting a brand new process, so I'm going to go with that approach:

app/email.py: Send emails asynchronously.

from threading import Thread
# ...

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

The send_async_email function now runs in a background thread, invoked via the Thread() class in the last line of send_email(). With this change, the sending of the email will run in the thread, and when the process completes the thread will end and clean itself up. If you have configured a real email server, you will definitely notice a speed improvement when you press the submit button on the password reset request form.

You probably expected that only the msg argument would be sent to the thread, but as you can see in the code, I'm also sending the application instance. When working with threads there is an important design aspect of Flask that needs to be kept in mind. Flask uses contexts to avoid having to pass arguments across functions. I'm not going to go into a lot of detail on this, but know that there are two types of contexts, the application context and the request context. In most cases, these contexts are automatically managed by the framework, but when the application starts custom threads, contexts for those threads may need to be manually created.

There are many extensions that require an application context to be in place to work, because that allows them to find the Flask application instance without it being passed as an argument. The reason many extensions need to know the application instance is because they have their configuration stored in the app.config object. This is exactly the situation with Flask-Mail. The mail.send() method needs to access the configuration values for the email server, and that can only be done by knowing what the application is. The application context that is created with the with app.app_context() call makes the application instance accessible via the current_app variable from Flask.

248 comments

  • #226 Miguel Grinberg said 2020-07-31T09:25:56Z

    @Md: your email configuration is incorrect.

  • #227 Alessandro said 2020-08-02T06:51:35Z

    Hi Miguel, thanks for the great tutorial! I'm getting this long error stack when submitting request for password recovery. Any idea what the reason could be? Thanks so much, have a great day!

    Traceback (most recent call last): File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask_compat.py", line 39, in reraise raise value File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint File "C:\Users\aless\Desktop\AI\CORSI\web_flask\microblog\app\routes.py", line 217, in reset_password_request send_password_reset_email(user) File "C:\Users\aless\Desktop\AI\CORSI\web_flask\microblog\app\email.py", line 15, in send_password_reset_email send_email('[Microblog] Reset Your Password', File "C:\Users\aless\Desktop\AI\CORSI\web_flask\microblog\app\email.py", line 11, in send_email mail.send(msg) File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask_mail.py", line 491, in send with self.connect() as connection: File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask_mail.py", line 144, in enter self.host = self.configure_host() File "C:\Users\aless\Anaconda3\envs\flask_env\lib\site-packages\flask_mail.py", line 158, in configure_host host = smtplib.SMTP(self.mail.server, self.mail.port) File "C:\Users\aless\Anaconda3\envs\flask_env\lib\smtplib.py", line 253, in init (code, msg) = self.connect(host, port) File "C:\Users\aless\Anaconda3\envs\flask_env\lib\smtplib.py", line 339, in connect self.sock = self._get_socket(host, port, self.timeout) File "C:\Users\aless\Anaconda3\envs\flask_env\lib\smtplib.py", line 308, in _get_socket return socket.create_connection((host, port), timeout, File "C:\Users\aless\Anaconda3\envs\flask_env\lib\socket.py", line 808, in create_connection raise err File "C:\Users\aless\Anaconda3\envs\flask_env\lib\socket.py", line 796, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it Message: 'Exception on /reset_password_request [POST]' Arguments: () 127.0.0.1 - - [01/Aug/2020 15:43:52] "?[35m?[1mPOST /reset_password_request HTTP/1.1?[0m" 500 -

  • #228 Miguel Grinberg said 2020-08-02T13:44:55Z

    @Alessandro: your email configuration must be incorrect. The error indicates that the server that you named as your email server is refusing a connection.

  • #229 Ifeanyi said 2020-08-28T18:57:30Z

    Hello Miguel, Thank you for this great series! However after entering an email and hitting reset i get this error

    127.0.0.1 - - [28/Aug/2020 19:41:28] "?[37mGET /reset_password_request HTTP/1.1?[0m" 200 - 127.0.0.1 - - [28/Aug/2020 19:41:37] "?[35m?[1mPOST /reset_password_request HTTP/1.1?[0m" 500 - Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 2464, in call return self.wsgi_app(environ, start_response) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask_compat.py", line 39, in reraise raise value File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask_compat.py", line 39, in reraise raise value File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint File "c:\Users\Desktop\Work Files\my_projects\python_\microblog\app\routes.py", line 92, in reset_password_request send_password_reset_email(user) File "c:\Users\Desktop\Work Files\my_projects\python_\microblog\app\email.py", line 18, in send_password_reset_email html_body=render_template('email/reset_password.html', user=user, token=token)) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py", line 140, in render_template ctx.app, File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py", line 120, in render rv = template.render(context) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py", line 1090, in render self.environment.handle_exception() File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py", line 832, in handle_exception reraise(*rewrite_traceback_stack(source=source)) File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2_compat.py", line 28, in reraise raise value.with_traceback(tb) File "c:\Users\Desktop\Work Files\my_projects\python\microblog\app\templates\email\reset_password.html", line 1, in top-level template code {% extends "base.html" %} File "c:\Users\Desktop\Work Files\my_projects\python_\microblog\app\templates\base.html", line 32, in top-level template code {% block content %} File "c:\Users\Desktop\Work Files\my_projects\python_\microblog\app\templates\email\reset_password.html", line 6, in block "content" {{ form.hidden_tag() }} File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py", line 471, in getattr return getattr(obj, attribute) jinja2.exceptions.UndefinedError: 'form' is undefined

    i need help fixing this please

  • #230 Miguel Grinberg said 2020-08-29T10:43:25Z

    @Ifeanyi: you haven't passed the form variable as template argument in the render_template() call.

  • #231 ifeanyi said 2020-08-30T10:39:10Z

    Thank you miguel. I was able to fix the error by creating the reset_password.html file in the templates folder which i omitted in error.

    But here's another thing, after clicking the request password reset button i get the flash to check my email. Nothing pops up on the local smtp server. Also from the terminal, i get a 302 returned for the post request of reset_password request i made. How do i go about that?

  • #232 Miguel Grinberg said 2020-08-30T12:37:47Z

    @ifeanyi: the code for this chapter is available from the download links at the top. Have you compared your code against this? That should help you find the mistake, I can't really tell you what's missing from your description, it could be a number of things.

  • #233 yj said 2020-09-05T19:29:04Z

    Hi miguel, and thanks so much for the tutorial. I am currently testing out the password reset feature. However, after typing in the new password and hitting 'change password', I get a 'TypeError: Object of type User is not JSON serializable' error. 127.0.0.1 - - [06/Sep/2020 03:14:40] "POST /reset_password/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyZXNldF9wYXNzd29yZCI6NiwiZXhwIjoxNTk5MzQyMTg1LjU5ODAxNTh9.4P1rrkEMS0LP0Y2-3Hjx74rxvqX9fT1lX2w_JL_jDPM HTTP/1.1" 500 - Traceback (most recent call last): File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 2464, in call return self.wsgi_app(environ, start_response) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask_compat.py", line 39, in reraise raise value File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 1953, in full_dispatch_request return self.finalize_request(rv) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 1970, in finalize_request response = self.process_response(response) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\app.py", line 2269, in process_response self.session_interface.save_session(self, ctx.session, response) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\sessions.py", line 378, in save_session val = self.get_signing_serializer(app).dumps(dict(session)) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\itsdangerous\serializer.py", line 166, in dumps payload = want_bytes(self.dump_payload(obj)) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\itsdangerous\url_safe.py", line 42, in dump_payload json = super(URLSafeSerializerMixin, self).dump_payload(obj) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\itsdangerous\serializer.py", line 133, in dump_payload return want_bytes(self.serializer.dumps(obj, self.serializer_kwargs)) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\json\tag.py", line 305, in dumps return dumps(self.tag(value), separators=(",", ":")) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\json__init__.py", line 211, in dumps rv = _json.dumps(obj, kwargs) File "C:\Users\flame.conda\envs\flask_fidel\lib\json__init.py", line 234, in dumps return cls( File "C:\Users\flame.conda\envs\flask_fidel\lib\json\encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "C:\Users\flame.conda\envs\flask_fidel\lib\json\encoder.py", line 257, in iterencode return _iterencode(o, 0) File "C:\Users\flame.conda\envs\flask_fidel\lib\site-packages\flask\json__init.py", line 100, in default return _json.JSONEncoder.default(self, o) File "C:\Users\flame.conda\envs\flask_fidel\lib\json\encoder.py", line 179, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type User is not JSON serializable

    I couldn't find any differences between my code and yours. Thanks!

  • #234 Miguel Grinberg said 2020-09-05T21:25:43Z

    @yj: I suggest that you compare your code against mine, because for some reason you are getting your session populated with invalid contents. If you have made changes that deviate from this project's code, pay special attention to data that you add to the user session, which should only have basic types that are compatible with JSON.

  • #235 Gitau Harrison said 2020-09-06T05:49:29Z

    Every step of setting up email validation works quite well, except for one last part:

    smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f1sm13899323wrx.75 - gsmtp')

    I have allowed insecure apps to run on my GMAIL but I am not so sure where the incorrect Username and password come from, because within the config.py file where email variables are set, the ADMIN 'Username' and 'password' are accurate. Does that mean that the user logging into the app and trying to rest their passwords have to supply equally accurate 'Username' and 'password' rather than dummy ones?

    MAIL_USERNAME and MAIL_PASSWORD are set very accurately and a valid ADMIN email is provided.

    I am not certain how to go about getting rid of the "smtplib.SMTPAuthenticationError: (535, b'5.7.8...BadCredentials)" error

  • #236 Miguel Grinberg said 2020-09-06T13:27:30Z

    @Gitau: this means that the email credentials that you are passing to Flask-Mail are incorrect. I know you claimed they are correct, but Google disagrees, so you need to figure out why Google does not like the credentials that you are sending. It is not a problem with the insecure apps setting, because that has a different error message.

  • #237 Gitau Harrison said 2020-09-13T11:27:39Z

    This is the email configurations set up:

    MAIL_SERVER = 'smtp.gmail.com' MAIL_PORT = 587 MAIL_USE_TLS = 1 MAIL_USERNAME = 'username' MAIL_PASSWORD = 'password' ADMINS = ['example_admin_email@gmail.com']

    I have tried: MAIL_USERNAME = 'admin username' MAIL_PASSWORD = 'admin password' ADMIN = ['admin_email@gmail.com']

    User password is admin_email@gmail.com (he is a user of the app) This generates the error:

    smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f1sm13899323wrx.75 - gsmtp')

    I have also tried: MAIL_USERNAME = 'recipient username' MAIL_PASSWORD = 'recipient password' ADMIN = ['admin_email@gmail.com']

    User password is recipient_email@gmail.com (he is a user of the app) This generates the error:

    smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f1sm13899323wrx.75 - gsmtp')

    I have also tried: MAIL_USERNAME = 'recipient username' MAIL_PASSWORD = 'admin password' ADMIN = ['admin_email@gmail.com'] User password is recipient_email@gmail.com (he is a user of the app) This generates the error:

    smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f1sm13899323wrx.75 - gsmtp')

    In all instances, the details are accurate.

    For MAIL_USERNAME = 'username' and MAIL_PASSWORD = 'password', do I set the admin's accurate username and password or is it the recipient's username and password and request for a password reset using recipient/admin's email?

  • #238 Miguel Grinberg said 2020-09-13T17:01:11Z

    @Gitau: the username and password are those for your Google account. Google does not like the credentials that you are sending. Unfortunately there isn't much I can do to help, it appears you are sending incorrect values.

  • #239 Kieron said 2020-09-19T13:10:38Z

    Hey miguel

    If you were to want to allow users to send emails to one another through the app could you skip the config part by implementing the function send_email('Email from in app', sender=[user.email], recipients=[recipient.email], text_body=form.body.data) Assuming we have the users email saved and a form with route organized? Sort of like the. Private messaging system?

    Would this simplify allowing users to send external emails to one another without needing to set thee mail server config or would it still be necessary? Consideringg the two users one may be using a Gmail acc and the other a webmail or Hotmail etc?

  • #240 Miguel Grinberg said 2020-09-19T13:25:40Z

    @Kieron: the Flask-Mail extension works with SMTP, so you would still need to set up an SMTP server to deliver emails. The other option would be to not use Flask-Mail and send emails in some other form, like maybe invoking the "mail" command. Note that one of the final chapters in this tutorial shows how to implement private messaging between users.

  • #241 Paul E. Johnson said 2020-10-02T20:16:07Z

    I want to test this in the python shell, but I end up with an error that I'm not in an app context. So I changed to flask shell and code runs without any errors. However, no email appears in recipient account.

    I have created environment variables for

    MAIL_SERVER=... MAIL_PORT=587 MAIL_USE_TLS=True MAIL_USERNAME=... MAIL_PASSWORD=...

    $ flask shell OS environment development OS environment is DEVELOPMENT: development fname app.db basedir /home/pauljohn/GIT/flask/pj_flask/base app.register now: main_bp reached Python 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0] on linux App: app [debug] Instance: /home/pauljohn/GIT/flask/pj_flask/base/instance &gt;&gt;&gt; from flask_mail import Message &gt;&gt;&gt; from app import mail &gt;&gt;&gt; msg = Message('test subject', sender="pauljohn32@freefaculty.org", recipients=['pauljohn@ku.edu']) &gt;&gt;&gt; msg.body = "asdf asdfka sdfjlas jaksdlf jasdklf" &gt;&gt;&gt; msg.html = "&lt;h1&gt;HTML inside here&lt;/h1&gt;" &gt;&gt;&gt; mail.send(msg)

    Can you tell how to debug?

  • #242 Paul E. Johnson said 2020-10-02T20:20:40Z

    I see some people with gmail smtp trouble. The problem may be caused by new security defaults at Google. Most accounts are automatically changed to the "more secure" policy, so that people who want to use their smtp server should create the 16 character unique password for this individual purpose. The generally used gmail password is not acceptable for that. I can give details if this continues to be a problem. The key step is to ignore the google website's advice that you don't need this special one time, single app password. You do need it, there's no alternative. Also possible to change your google account to the "less secure" setting, but I work for a company where that's not allowed.

  • #243 shazib said 2020-10-10T17:52:01Z

    hy, Miguel sir i want to ask that what i have done wrong with it that after adding email and request for password reset button application render on login page and show flash msg Check your email for the instruction to reset your password, but in command prompt this error raise i could not figure out why username and password are not accepting.

    raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials 10sm15988761wmn.9 - gsmtp') 127.0.0.1 - - [10/Oct/2020 22:44:14] "GET /index HTTP/1.1" 302 - 127.0.0.1 - - [10/Oct/2020 22:44:14] "GET /login?next=%2Findex HTTP/1.1" 200 - 127.0.0.1 - - [10/Oct/2020 22:44:14] "GET /favicon.ico HTTP/1.1" 404 -

  • #244 Miguel Grinberg said 2020-10-10T19:35:14Z

    @shazib: The credentials you configured for your email server are incorrect.

  • #245 shazib said 2020-10-12T15:04:35Z

    hy, Miguel sir STILL giving error

    raise SMTPServerDisconnected('please run connect() first') smtplib.SMTPServerDisconnected: please run connect() first 127.0.0.1 - - [12/Oct/2020 20:02:21] "GET /favicon.ico HTTP/1.1" 404 -

  • #246 Miguel Grinberg said 2020-10-13T08:27:53Z

    @shazib: did you set your email credentials?

  • #247 Jac said 2020-10-26T09:41:37Z

    Hallo Miguel: Thank you again for these tutorials.

    I have successfully used the Debugging Server to send the reset password request. I tried to paste the URL for reset the password response into a browser but I noticed that the token wasn't there (possibly correct because it is after all a security measure). So I print()ed out the token and added it tot he URL thus:- http://192.168.1.208:8088/reset_password%28token%29?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyZXNldF9wYXNzd29yZCI6NCwiZXhwIjoxNjAzNjIzMjc3Ljk3Mzc0NH0.HWs0iiFEFO2IXbnsuV1l68ocRcIdoiW2RPR8E-A18oA;external=True This didn't work. I also added &amp and & after the token= element of the URL without success. Is it possible to use the DebuggingServer in this way or do I have to try to find my way past the security of my email provided to fully test the code?

  • #248 Miguel Grinberg said 2020-10-26T15:19:31Z

    @Jac: what is the ;external=True thing at the end of your URL? That shouldn't be there.

Leave a Comment