email | Python Standard Library – Real Python

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:

Key Features

  • Creates and manipulates email messages
  • Parses email messages from strings or files
  • Supports MIME and other RFC 5322-based formats
  • Manages email headers and attachments

Frequently Used Classes and Functions

Examples

Creating and manipulating email messages:

Parsing an email from a string:

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:

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.

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.

intermediate web-dev

For additional information on related topics, take a look at the following resources: