avatarYang Zhou

Summary

This article provides a guide on how to send three types of emails using Python, including plain text emails, HTML content emails, and emails with attached files.

Abstract

The article titled "Email Automation in Python: Sending These 3 Types of Emails Effortlessly" provides a comprehensive guide on automating email tasks using Python. The author begins by explaining the power of Python as a tool for automating boring tasks and improving productivity. The article focuses on three common types of emails: plain text emails, HTML content emails, and emails with attached files. The author provides examples and code snippets for each type of email, using built-in Python modules such as smtplib and email.mime. The article emphasizes the importance of making emails more complex and better formatted by using HTML content and sending plain text versions alongside HTML versions to ensure that recipients can understand the message. Finally, the author concludes by highlighting the simplicity of sending emails with attached files using the email.mime module.

Bullet points

  • Python is a powerful tool for automating boring tasks and improving productivity.
  • The article provides a guide on sending three types of emails using Python.
  • The first type of email is a plain text email, which is simple to send using Python's built-in smtplib module.
  • The second type of email is an HTML content email, which is more complex and better formatted. The author recommends sending plain text versions alongside HTML versions to ensure that recipients can understand the message.
  • The third type of email is an email with attached files, which is simple to send using the email.mime module.
  • The article provides code snippets for each type of email and emphasizes the importance of making emails more complex and better formatted.

Email Automation in Python: Sending These 3 Types of Emails Effortlessly

Automate some boring stuff with Python

Photo by Brett Jordan on Unsplash

Python is not only a programming language for data science, it’s also a powerful tool which can automate lots of boring tasks and make you 10x productive.

Imagine that you have a crush on a pretty girl and want to say good morning to her by email every day. Unfortunately, you are not an early bird and literally get up at noon every day.

Is there any chance that you can sleep enough and send the important emails at the same time?

Yes, no problem at all. Python can help you. 🙂

All you need to do is writing a Python script to send emails automatically and schedule this script on a server for executing it every morning. Then, enjoy your sleep as always. 😴

This article, which is a helpful reference for you to chase your girl, will introduce how to send 3 common types of emails with Python:

  • Emails that contain plain texts
  • Better formatted emails that use HTML content
  • Emails with attached files

After reading, you will literally have higher possibilities to get a response from her.

1. Send Plain-Text Emails in Python

Simply put, there are two steps to send an email in Python:

  • Connect and login a mail server
  • Send the email from the mail server to the recipient

Mail servers use the Simple Mail Transfer Protocol (SMTP) to send emails to one another across the Internet. Python has a built-in module called smtplib which provide relative methods to help us send emails. In addition, to make everything secure, we need the secure sockets layer (SSL) protocol to encrypt an SMTP connection. Python also has the built-in module named ssl.

Based on the above two modules, we can implement the two steps like the following:

As shown above, we only need to provide the address and port of our mail server and use an email address and password to login. Then, only one line of code server.sendmail(FROM, TO, MSG) is needed to send a simple plain-text email.

2. Send Emails Including HTML Contents

Sometimes, we need to send more complex content, such as one sentence with a hyperlink, bold or italic words, and better-formatted content. In these cases, HTML contents are necessary.

Python also has built-in modules to make everything easier. Let’s see an example:

As shown above, we use the MIMEMultipart object to make two versions of messages, one is plain text and another is the HTML content. Every email client that can render HTML will display the html_msg, otherwise it will display the text_msg.

Although we would like to send the HTML version message only, sending the plain text version at the same is a recommended way. So even if the recipient is using a tool which can’t render HTML, he or she can still receive the plain text version and understand what you sent.

3. Send Emails with Attached Files

The email.mime module in Python is really helpful. With the help of it, we only need to encode and attach the file into our email’s message:

Yes, it’s really as simple as the above to send an email with attached files. For more details of the email.mime module, its official document will never let you down.

Conclusion

Automation is one of Python’s superpower. We can use Python to automate lots of boring and repeating things easily, even don’t need to install third-party modules. To send emails, we only need to be familiar with the built-in email.mime and smtplib modules.

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles about programming and technologies!

Reading more:

Python
Programming
Coding
Technology
Data Science
Recommended from ReadMedium