English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Flask Send Email

Web-based applications usually need to have the ability to send emails to users/The function of sending emails from the client. Flask-The Mail extension makes it very easy to set up a simple interface for any email server

Initially, Flask-The Mail extension can be installed using the pip tool, as shown below -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
pip install Flask-Mail

Then you need to configure Flask by setting the value of the following application parameters-Mail.

NumberParametersDescription
1MAIL_SERVERThe name of the mail server/IP address
2MAIL_PORTThe port number of the server used
3MAIL_USE_TLSEnable/Disable Transport Layer Security encryption
4MAIL_USE_SSLEnable/Disable Secure Sockets Layer encryption
5MAIL_DEBUGDebug support, the default is the debug status of the Flask application
6MAIL_USERNAMEThe username of the sender
7MAIL_PASSWORDThe password of the sender
8MAIL_DEFAULT_SENDERSet the default sender
9MAIL_MAX_EMAILSSet the maximum number of emails to be sent
10MAIL_SUPPRESS_SENDIf app.testing is set to true, the sent email is suppressed
11MAIL_ASCII_ATTACHMENTSIf set to true, the attached file name is converted to ASCII

flask-The mail module contains the definitions of the following important classes.

Mail class

It manages the requirements of email messages. The class constructor takes the following form -

NumberMethodDescription
1send()Send the content of the Message class object
2connect()Open a connection to the email host
3send_message()Send the message object

Message class

It encapsulates an email, the constructor of the Message class has several parameters -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
flask-mail.Message(subject, recipients, body, html, sender, cc, bcc, 
    reply-to, date, charset, extra_headers, mail_options, rcpt_options)

Message class method

attach() - Attach a file to the message. This method takes the following parameters - filename - The name of the file to be attached content_type - The MIME type of the file data - Original file data disposition - Content handling, if any.

add_recipient() - Add another recipient to the message

In the following example, the SMTP server of Google's Gmail service is used as the Flask-The MAIL_SERVER configured by Mail.

No.1Step - Import from flask-The mail module imports the Mail and Message classes.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from flask_mail import Mail, Message

No.2Step - Then configure Flask according to the following settings-Mail.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
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

No.3Step - Create an instance of the Mail class.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
mail = Mail(app)

No.4Step - In the Python function mapped by URL rules (/Set the Message object in the parentheses.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
@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"

No.5Step - The entire code is as follows. Run the following script in Python Shell and access the URL: http://localhost:5000/.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
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)

Please note that built-in insecure features in Gmail services may block this login attempt, and you may need to reduce the security level. Please log in to your Gmail account and access  This linkto reduce security.