English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
Number | Parameters | Description |
1 | MAIL_SERVER | The name of the mail server/IP address |
2 | MAIL_PORT | The port number of the server used |
3 | MAIL_USE_TLS | Enable/Disable Transport Layer Security encryption |
4 | MAIL_USE_SSL | Enable/Disable Secure Sockets Layer encryption |
5 | MAIL_DEBUG | Debug support, the default is the debug status of the Flask application |
6 | MAIL_USERNAME | The username of the sender |
7 | MAIL_PASSWORD | The password of the sender |
8 | MAIL_DEFAULT_SENDER | Set the default sender |
9 | MAIL_MAX_EMAILS | Set the maximum number of emails to be sent |
10 | MAIL_SUPPRESS_SEND | If app.testing is set to true, the sent email is suppressed |
11 | MAIL_ASCII_ATTACHMENTS | If set to true, the attached file name is converted to ASCII |
flask-The mail module contains the definitions of the following important classes.
It manages the requirements of email messages. The class constructor takes the following form -
Number | Method | Description |
1 | send() | Send the content of the Message class object |
2 | connect() | Open a connection to the email host |
3 | send_message() | Send the message object |
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)
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.