How to Send Emails with Python

Design Professional Mails using Flask!

SAGAR SHARMA
4 min readOct 24, 2018

Have you ever wondered how companies like Medium send Emails to users that look so fancy that it is basically a website in a mail? And on top of that — they send millions of Emails to their Massive user-base in minutes DAILY!!! All Customized to the user's interests.

and if you don’t know JUST OPEN ANY OF YOUR EMAIL TAGGED PROMOTIONS

They have Images, Subscribe Buttons, Tables, GIF’s, Booking Buttons, Headers (in the center), Logos, Amazing Fonts, Socials, and god knows what else.

So let’s make our own “Automatic-Pro-PythonCode-Email-Sender-Bot…ish”

Let’s write a few lines of Python code that will allow us to sent emails to 1000’s of people at once just by running that python script.

Lets pip First

We are going to use Flask and Flask-mail to do this. So open your command prompt & do.

pip install Flask Flask-Mail

Code 💻

You only need 3 easy steps for this:

1. Create a Flask App

Let's start by creating a Flask app.

We will be using mail variable to manage the features the instance “Mail” present in the Flask-Mail library.

2. Add Your Details

Configure Details

3. Compose & Send [Simple]

Message Body
OUTPUT

🍓 Compose & Send [Advanced]

By using .html instead of .body you can use different html tags and add .svg, .png too. I am using <img> argument to add image using Url.

Message .HTML

🍓 Compose & Send [Pro]

Now that we know how add text & image. We can use different .html tags in combination with text and images to make it impressive like any other company Email. Also add <button> </button> redirecting users to your new websites.

Book Tickets 🎫 button redirects you to the website www.bookmyshow.com by opening a new window.

Get the complete code on GitHub and try it yourself.

💁 Keep in Mind

Since our code is not a full fledged app at this point, you may need to toggle your Gmail Setting.

Turn it ON!

Keep it ON

How can I Send Personalized Email to 1000’s of Users?!

Automate Mails 📬

(like) Change User’s Name — Use python to go through a .txt or excel document of your user base and parse through all the names one by one and replace it in the mail.

!dea: You can also change all the “he” to “she” or vice-versa depending on the gender.

How can I Attach Files with the Mail ?!

Attaching Files 📎

Here we are attaching image.png file present in the same folder. You can also add pdf file by changing the name and extension.

--

--