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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Sending bulk emails in Node.js using SendGrid API
- How to read Emails from Gmail using Gmail API in Python ?
- Sending SMS using NEXMO API in Node.js
- How to get emails using PHP and IMAP ?
- PHP | Sending mails using mail() function
- Sending bulk SMS in Node.js using Twilio
- Python | Fetch your gmail emails from a particular user
- Python - Send email to a list of emails from a spreadsheet
- Fetch Unseen Emails From Gmail Inbox
- Web scraper for extracting emails based on keywords and regions
- Sending Push Notifications : Progressive Web Apps
- Setup Sending Email in Django Project
- Automatic Birthday mail sending with Python
- Difference between REST API and SOAP API
- Speech Recognition in Python using Google Speech API
- Python | Get a set of places according to search query using Google Places API
- Get emotions of images using Microsoft emotion API in Python
- Python | URL shortener using tinyurl API
- Python | Find current weather of any city using openweathermap API
- Fetching top news using News API
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.