Published on

Python Email Attachments Made Easy: A Step-by-Step Guide to Sending Files via Email


Table of contents

Sending email attachments can be a common requirement in many Python applications. Whether you need to send reports, images, or other types of files, Python provides powerful libraries to help you accomplish this task. In this guide, we'll walk you through the process of sending email attachments with Python, step-by-step.


Before we get started, you'll need to have the following prerequisites in place:

Step 1: Import the Necessary Libraries

To send email attachments with Python, you'll need to use two main libraries: email and smtplib. The email library provides classes for creating and formatting email messages, while smtplib provides classes for sending email messages via SMTP (Simple Mail Transfer Protocol).

To get started, open a new Python file and import these libraries:

import smtplib
from email.message import EmailMessage

Step 2: Set Up Your Email Account Information

Next, you'll need to provide your email account information to Python. This includes your email address, password, and the SMTP server settings for your email service provider. Here's an example of how to set up a Gmail account:

# Set up email account information
email_address = ""
email_password = "your_password"
smtp_server = ""
smtp_port = 587

Be sure to replace your_email and your_password with your actual email address and password. If you're using a different email service provider, you'll need to use the appropriate SMTP server settings.

Step 3: Create Your Email Message

Now that you have your libraries imported and email account information set up, it's time to create your email message. To create an email message with attachments, you'll need to use the EmailMessage class from the email.message library.

Here's an example of how to create a simple email message with a subject, body, and attachment:

# Create email message
msg = EmailMessage()
msg['Subject'] = 'Test Email with Attachment'
msg['From'] = email_address
msg['To'] = ''
msg.set_content('Hello, this is a test email with an attachment.')
with open('file.txt', 'rb') as f:
    file_data =
msg.add_attachment(file_data, maintype='text', subtype='plain', filename='file.txt')

In this example, we're creating an email message with the subject "Test Email with Attachment," a sender of our own email address, a recipient of and a body text of "Hello, this is a test email with an attachment." We're also attaching a file named "file.txt" using the add_attachment method, which reads the file data and sets the attachment's content type to "text/plain."

Step 4: Send Your Email Message

Finally, it's time to send your email message with attachments using the smtplib library. Here's an example of how to send your email message using a Gmail account:

# Send email message
with smtplib.SMTP(smtp_server, smtp_port) as server:
    server.login(email_address, email_password)

In this example, we're using smtplib.SMTP to connect to the SMTP server for Gmail.

For setting up a secure connection with starttls() and logging in to our email account using login(). We then send the email message using send_message().

Additional Tips and Tricks

Here are some additional tips and tricks to help you work with email attachments in Python:

Adding Multiple Attachments

To add multiple attachments to your email message, simply call add_attachment() multiple times with different filenames and file data.

with open('file1.txt', 'rb') as f1, open('file2.txt', 'rb') as f2:
    file1_data =
    file2_data =
msg.add_attachment(file1_data, maintype='text', subtype='plain', filename='file1.txt')
msg.add_attachment(file2_data, maintype='text', subtype='plain', filename='file2.txt')

Specifying Attachment Content Types

The maintype and subtype arguments for add_attachment() determine the content type of the attachment. Here are some common content types:

  • Text: maintype='text', subtype='plain'
  • HTML: maintype='text', subtype='html'
  • Images: maintype='image', subtype='jpeg' or maintype='image', subtype='png'
  • PDFs: maintype='application', subtype='pdf'

Handling Large Attachments

If you're working with large attachments, you may run into memory issues when reading the file data into memory with read(). To avoid this, you can use the EmailMessage.iter_attachments() method to iterate over the attachment data in chunks.

with open('large_file.txt', 'rb') as f:
    while True:
        file_data =
        if not file_data:
        msg.add_attachment(file_data, maintype='text', subtype='plain', filename='large_file.txt')

Using HTML in Your Email Message

If you want to use HTML in your email message, simply set the message content to an HTML string using msg.set_content().

msg.set_content('<html><body><h1>Test Email with HTML</h1><p>This is a test email with HTML content.</p></body></html>', subtype='html')

Frequently Asked Questions

Can I send multiple attachments in one email with Python?
Yes, you can send multiple attachments in one email using Python's email and smtplib libraries. Simply create an EmailMessage object and add the attachments using the add_attachment method.

How do I handle errors when sending email attachments with Python?
When sending email attachments with Python, you may encounter errors related to network connectivity, authentication, or other issues. You can use exception handling to catch and handle these errors, and consider logging to help diagnose and resolve issues.

What file types can I send as email attachments with Python?
You can send a wide range of file types as email attachments with Python, including documents, spreadsheets, images, audio files, and more. However, be aware of file size limits and potential security concerns related to certain file types.

Can I use Python to receive email attachments?
Yes, you can use Python's imaplib library to receive email attachments from an IMAP server. You can search for specific messages, download attachments, and save them to your local file system.

How can I ensure the security of email attachments sent with Python?
To ensure the security of email attachments sent with Python, use a secure email service provider and follow best practices for password management, encryption, and network security. Additionally, be mindful of the potential risks associated with certain file types, and consider using file compression and encryption tools for added protection.

How do I test my email attachments to make sure they work?
To test your email attachments, create test messages and send them to yourself or other trusted recipients. Be sure to test attachments of different file types and sizes, and verify that they can be opened and viewed on different devices and operating systems.


Sending email attachments with Python can seem daunting at first, but with the email and smtplib libraries, it's actually quite easy. By following the steps outlined in this guide, you should now have a good understanding of how to create and send email messages with attachments in Python. Happy emailing!