Multipart MIME messages combine alternative plain-text and HTML presentations, and optionally attach files. Email clients choose appropriate alternatives based on supported content.
What You Will Learn
This lesson focuses on MIME Multipart Messages: Text, HTML, and Attachments. You will connect the operation to a concrete input, inspect the result and identify the situation in which a different approach would be safer or clearer.
An email workflow has separate steps for message construction, submission and mailbox retrieval. MIME controls the structure of the message, while SMTP and IMAP are network protocols for transporting or accessing mail.
How It Works
Prepare headers and body parts with EmailMessage rather than building raw MIME text by hand. Serialization converts that structure into a standards-compatible byte stream; delivery requires an independent network operation.
Worked Example
The following example isolates the main behavior so you can run it from a local Python installation. Read the inputs first, then follow the operation and the final printed result.
from email.message import EmailMessage
message = EmailMessage()
message["Subject"] = "Report"
message.set_content("Your report is ready.")
message.add_alternative("<p>Your report is <strong>ready</strong>.</p>", subtype="html")
message.add_attachment(b"id,total\n1,25\n", maintype="text", subtype="csv", filename="report.csv")
print(message.is_multipart())
print([part.get_content_type() for part in message.walk()])
Understanding the Example
Start with the setup and import statements; these define the objects that the operation needs. Follow each assignment or function call in order, noting which arguments are provided and whether the operation creates a new value, changes existing state or returns a result. Finally, inspect the printed value or assertion rather than assuming that the operation worked.
The important behavior demonstrated here is: Multipart MIME messages combine alternative plain-text and HTML presentations, and optionally attach files. Email clients choose appropriate alternatives based on supported content. The example uses deliberately small values so the intermediate result is easy to reason about. A larger application must still handle validation, errors and resource cleanup.
Example Output
True
['multipart/mixed', 'multipart/alternative', 'text/plain', 'text/html', 'text/csv']
This output was obtained by running the local example in the available Python environment. Verify that the values match your reasoning, then change one input and try again.
When to Use This Approach
Build and parse messages entirely offline before connecting to a test SMTP/IMAP service. This makes formatting errors easier to distinguish from connection or authentication problems.
Important Behavior and Edge Cases
Sending only HTML without a plain-text alternative, or treating attachment filenames as safe filesystem paths.
Mail addresses and attachments are untrusted metadata. Enforce message-size limits, validate attachment save locations, protect credentials and do not confuse SMTP acceptance with final delivery.
Practice Exercise
Add a small text attachment and inspect the multipart/alternative part inside the multipart/mixed message.
Before running your modified code, predict the output or expected failure. Confirm the result using at least one ordinary case and one invalid, missing or boundary input. If the operation accesses external resources, first exercise it against a controlled local or test environment.
Common Beginner Mistakes
- Sending only HTML without a plain-text alternative, or treating attachment filenames as safe filesystem paths.
- Mail addresses and attachments are untrusted metadata. Enforce message-size limits, validate attachment save locations, protect credentials and do not confuse SMTP acceptance with final delivery.
Key Takeaways
- Multipart MIME messages combine alternative plain-text and HTML presentations, and optionally attach files. Email clients choose appropriate alternatives based on supported content.
- Choose this technique for the problem it solves, but validate inputs and follow the cleanup and failure behavior shown above.
- Next practice step: Add a small text attachment and inspect the multipart/alternative part inside the multipart/mixed message.