Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

E-mail support for Flask applications


The ability to send e-mails 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 also 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 take a simple case where en e-mail will be sent to a catalog manager in the application whenever a new category is added.

How to do it…

First, we need to 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'] = 'gmail_password'
app.config['MAIL_DEFAULT_SENDER'] = ('Sender name', 'sender email')
mail...