email
The Python email
package allows for managing email messages, including creating, parsing, and manipulating email messages in MIME and other RFC 5322-based message formats. It provides a comprehensive API for handling email content, headers, and attachments.
Here’s a quick example:
>>> from email.message import EmailMessage
>>> msg = EmailMessage()
>>> msg["Subject"] = "Hello"
>>> msg.set_content("This is a test email.")
>>> print(msg)
Subject: Hello
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
This is a test email.
Key Features
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
EmailMessage |
Class | Represents an email message |
message_from_string() |
Function | Parses a string into an email message |
message_from_bytes() |
Function | Parses bytes into an email message |
Header |
Class | Represents an email header |
Examples
Creating and manipulating email messages:
>>> from email.message import EmailMessage
>>> msg = EmailMessage()
>>> msg["From"] = "sender@example.com"
>>> msg["To"] = "recipient@example.com"
>>> msg["Subject"] = "Test Email"
>>> msg.set_content("This is a plain text email.")
>>> print(msg.as_string())
From: sender@example.com
To: recipient@example.com
Subject: Test Email
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
This is a plain text email.
Parsing an email from a string:
>>> from email import message_from_string
>>> raw_email = "Subject: Test\n\nThis is a test email."
>>> msg = message_from_string(raw_email)
>>> print(msg["Subject"])
Test
Common Use Cases
- Creating and sending email messages programmatically
- Parsing email messages received from a server
- Extracting and modifying email headers and content
Real-World Example
Suppose you need to create an email with an attachment and send it using an SMTP server. Here’s a toy example of how you could do it. Note that the example is just demonstrative:
import os
import smtplib
from email.message import EmailMessage
from email.utils import formataddr
# Email configuration
address = os.getenv("EMAIL_USER")
password = os.getenv("EMAIL_PASS")
if not address or not password:
raise RuntimeError(
"EMAIL_USER or EMAIL_PASS environment variables not set"
)
# Create the email
msg = EmailMessage()
msg["Subject"] = "Monthly Report - June 2025"
msg["From"] = formataddr(("John Doe", address))
msg["To"] = "client@example.com"
msg.set_content(
"Hi,\n\nPlease find attached the report for June 2025.\n\nBests,\nJohn Doe"
)
# Attach the PDF
with open("monthly_report.pdf", "rb") as file:
file_data = file.read()
msg.add_attachment(
file_data,
maintype="application",
subtype="pdf",
filename="monthly_report.pdf"
)
# Send the email
with smtplib.SMTP_SSL("smtp.example.com", 465) as smtp:
smtp.login(address, password)
smtp.send_message(msg)
In this example, the email
package is used to create an email with an attachment, and the smtplib
module is used to send it over SMTP, showcasing the interoperability of Python’s standard library modules.
Related Resources
Tutorial
Sending Emails With Python
In this tutorial, you'll learn how to send emails using Python. Find out how to send plain-text and HTML messages, add files as attachments, and send personalized emails to multiple people.
For additional information on related topics, take a look at the following resources:
- Sending Emails Using Python (Course)
By Leodanis Pozo Ramos • Updated June 30, 2025