Python, being a powerful language don’t need any external library to import and offers a native library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. This article revolves around how we can send bulk customised emails to a group of people with the help of Flask.
Three packages are required for flask mail to work, Install then using pip,
pip install virtualenv
pip install Flask
3) Flask-Mail :
pip install Flask-Mail
After installing the packages, we have to use virtualenv (optional)
1) Create a virtualenv
Go to the folder you want to use for your project.
Write the following code:
python3 -m venv env (macOS/Linux) py -m venv env (Windows)
Here env is name of your environment.
2) Activate the environment
On windows :
On macOS/ Linux:
3) Make sure you get the (env) in the beginning displayed in picture below :
Flask-Mail is configured through the standard Flask config API. These are the available options (each is explained later in the documentation):
1) MAIL_SERVER : Name/IP address of the email server.
2) MAIL_PORT : Port number of server used.
3) MAIL_USE_TLS : Enable/disable Transport Security Layer encryption.
4) MAIL_USE_SSL : Enable/disable Secure Sockets Layer encryption
5) MAIL_DEBUG : Debug support. The default is Flask application’s debug status.
6) MAIL_USERNAME : Username of the sender
7) MAIL_PASSWORD : The password of the corresponding Username of the sender.
8) MAIL_ASCII_ATTACHMENTS : If set to true, attached filenames converted to ASCII.
9) MAIL_DEFAULT_SENDER : sets default sender
10) MAIL_SUPPRESS_SEND : Sending suppressed if app.testing set to true
11) MAIL_MAX_EMAILS : Sets maximum mails to be sent
Note : Not all of the configuration is to be set.
Sending Emails using Flask-Mail
Classes in Flask-Mail:
Mail Class : Manages email-messaging requirements
Message Class: encapsulates an email message
Let’s get our hands on the code.
Save it in a file and then run the script in Python Shell or CMD & Visit http://localhost:5000/.
Due to Google’s built-in security features, Gmail service may block this login attempt. You may have to decrease the security level. Visit https://myaccount.google.com/lesssecureapps?pli=1 to decrease security.
- Sending bulk emails in Node.js using SendGrid API
- Python | Fetch your gmail emails from a particular user
- Sending SMS using NEXMO API in Node.js
- Sending bulk SMS in Node.js using Twilio
- PHP | Sending mails using mail() function
- Sending Push Notifications : Progressive Web Apps
- How to Create an Image Overlay Icon using HTML and CSS ?
- How to Vertically Align Text Next to an Image using CSS ?
- How to Create ToDo App using ReactJS ?
- How to Remove URL from Printing the Page ?
- How to get city name by using geolocation ?
- Difference between Web Designer and Web Developer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.