O'Reilly logo

Automate it! - Recipes to upskill your business by Chetan Giridhar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

E-mail messages with attachments

One of the most used and simple use cases with e-mail is the ability to add attachments to your e-mail messages. In this section, we will learn how to add attachments to our e-mails in Python.

Getting ready

We use the same smtplib and email modules for this example. So, don't bother about the modules to be installed. Let's get on with the recipe.

How to do it...

  1. Let's begin by quickly creating a small text file. We will call it attach.txt, and its contents are shown in the following screenshot:

    How to do it...

  2. Let's look at the code that will help add an attachment to our e-mail:
     import smtplib from email.mime.multipart import MIMEMultipart ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required