How to send email via python

Send sales reports automatically every day

Kinder Sham
Analytics Vidhya
3 min readJun 15, 2020

--

Photo by Muukii on Unsplash

Introduction

Recently, my company has tasked me with looking into reporting solutions for our sales department. I need to send sales report to the sales manager every day. The sales report will be prepared after end of the day, and the report needs to be sent at 08:00 am. Therefore, I am trying to automate a flow using a custom scenario script written in Python.

How to send email via python

Python includes several modules in the standard library for working with emails and email servers. Basically we will use the built-in smtplib module, which can send email using Simple Mail Transfer Protocol (SMTP). smtplib uses RFC 821 protocol for SMTP. The examples in this tutorial will use the own SMTP server to send emails with attachments and HTML content, but the same principles apply to other email services.

To send the email, you need to make use of SMTP (Simple Mail Transfer Protocol). As mentioned earlier, Python provides libraries to handle this task you don’t need to extra installations or tricks are required. You can import the module using the following statement:

line 4–5: Import the smtplib related module. It should be included in Python by default.

line 6–8: We will deal with the MIME message type, which is able to combine HTML and plain text. In Python, it is handled by the email.mime module.

line 11: Attachments are still the MIME objects but we need to encode them with the base64 module.

line 13: Import the datetime module.

Let’ create the sendemail function:

line 2–6: Create the MIMEMultipart message object and load it with appropriate headers for From, To, and Subject fields.

line 9: Add your message body.

line 11–24: Add your attachment.

line 29–34: Login to the mail server and send the email out.

I recommend defining the email addresses and message content at the top of your script, then you can change it easily:

line 2: Define the sending date.

line 4–8: Define the sender name, sender email, sender passwd, recipient and subject.

line 10: Create a HTML messages.

line 14: call the sendemail function.

Cheers!!! I got the email with the sales report now!

Now we need to configure automatic report sending.

Create a run.sh

Setup the cron job at linux.

These are just basic options for sending email using Python. You can also use for…loop to send more than one recipient. For get the better results, please check the Python documentation!

Thanks for reading! If you enjoyed the post, please appreciate your support by applauding via the clap (👏🏼) button below or by sharing this article so others can find it.

In the end, I hope that you can learn how to using python to send an email. You can also find the full project on the GitHub repository.

--

--

Kinder Sham
Analytics Vidhya

Data scientist, cycling and game player enthusiast. Focus on how to use data science to answer questions.