Flask Mail

The final Flask extension that this chapter will cover is Flask Mail, which allows you to connect and configure your SMTP client from Flask's configuration. Flask Mail will also help to simplify application testing in Chapter 12, Testing Flask Apps. The first step is to install Flask Mail with pip:

$ pip install Flask-Mail

Next, the Mail object needs to be initialized in the extentions.py file:

from flask_mail import Mail

mail = Mail()

flask_mail will connect to our SMTP server of choice by reading the configuration variables in our app object, so we need to add those values to our config object:

class DevConfig(Config):

    MAIL_SERVER = 'localhost'
    MAIL_PORT = 25
    MAIL_USERNAME = 'username'
    MAIL_PASSWORD = 'password'

Finally, the mail object ...

Get Mastering Flask now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.