Implementing email support
The ability to send emails is usually one of the most basic functions of any web application. It is usually easy to implement with any application. With Python-based applications, it is quite simple to implement with the help of smtplib
. In the case of Flask, this is further simplified by an extension called Flask-Mail
.
Getting ready
Flask-Mail
can be easily installed via pip
:
$ pip install Flask-Mail
Let’s look at a simple case where an email will be sent to a catalog manager in the application whenever a new category is added.
How to do it…
First, instantiate the Mail
object in our application’s configuration – that is, my_app/__init__.py
:
from flask_mail import Mail app.config['MAIL_SERVER'] = 'smtp.gmail.com' app.config['MAIL_PORT'] = 587 app.config['MAIL_USE_TLS'] = True app.config['MAIL_USERNAME'] = 'gmail_username' app.config['MAIL_PASSWORD...