Web-based applications typically require the ability to send mail to the user/client. Flask doesn't have an out of the box solution to send mail.
Instead, the Flask-Mail extension makes it easy to establish a simple interface with any email server.
Email generally uses two protocols, one for sending mails (smtp) and one for receiving mail (pop3). This article is about sending mails.
Practice now: Test your Python skills with interactive challenges
Introduction
First, the Flask-Mail extension should be installed with the help of the pip utility.
pip install Flask-Mail
You then need to configure the Flask-Mail by setting the values for the following application parameters.
- MAIL_SERVER
- MAIL_USE_TLS
- MAIL_USE_SSL
- MAIL_DEBUG
- MAIL_USERNAME
- MAIL_PASSWORD
- MAIL_DEFAULT_SENDER
- MAIL_MAX_EMAILS
- MAIL_SUPPRESS_SEND
- MAIL_ASCII_ATTACHMENTS
The flask-mail module contains definitions of the following important classes.
The Mail class manages email messaging requirements.The class constructor takes the form of:
flask-mail.Mail(app = None)
Mail class methods include: send(), connect() and send_message().
The Message class encapsulated an email.The Message class constructor has several parameters:
flask-mail.Message(subject, recipients, body, html, sender, cc, bcc,
reply-to, date, charset, extra_headers, mail_options, rcpt_options)
Message class method attach () - Add attachment for mail.This method takes the following parameters:
- filename : The name of the file
- content_type : MIME type
- data - file data
You can use add_recipient() to add another recipient to the message.
Mail config and functions
In the example below, the SMTP server for the Google gmail service is used as the MAIL_SERVER for the Flask-Mail configuration.
Step 1: Import the Mail and Message classes from the flask-mail module in the code.
from flask_mail import Mail, Message
Step 2: Configure server parameters
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
Step 3: Create an instance of the Mail class.
mail = Mail(app)
Step 4: The Message object is set in a Python function that is mapped by the URL rule ('/').
@app.route("/")
def index():
msg = Message('Hello', sender = '[email protected]', recipients = ['[email protected]'])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
Flask mail example
The entire code is as follows.Run the following script in the Python shell and access localhost:5000/.
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
mail= Mail(app)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
@app.route("/")
def index():
msg = Message('Hello', sender = '[email protected]', recipients = ['[email protected]'])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run(debug = True)
Note that security in the Gmail service can prevent this login attempt. If you use gmail smtp server, you might have to reduce the security level.
Practice now: Test your Python skills with interactive challenges