Content · Programming · Python

Python Send a Plain-Text and HTML Multipart Message

A multipart message can offer text and HTML versions so different mail clients can display the same content.

2 views Published Oct 18, 2026 Updated Oct 18, 2026 Admin

A multipart message can offer text and HTML versions so different mail clients can display the same content.

Why This Matters


Connect the protocol to its actual role. SMTP is for submission, IMAP/POP are for retrieval, while FTP is a separate file-transfer protocol. Authentication and transport encryption are separate concerns.

In this lesson, the focus is Send a Plain-Text and HTML Multipart Message. The aim is to understand its inputs, observable behavior, and a realistic situation where it is useful, rather than memorize a command name.

How the Example Works


set_content defines the plain-text alternative; add_alternative attaches HTML without discarding the text version.

Step-by-Step Example


from email.message import EmailMessage
message = EmailMessage()
message["Subject"] = "Status"
message.set_content("Status: complete")
message.add_alternative("<p>Status: <strong>complete</strong></p>", subtype="html")
print(message.get_content_type())
print([part.get_content_type() for part in message.iter_parts()])

Understanding the Result


  1. The program starts by importing email.message, making the needed interfaces available.
  2. Watch these operations in order: EmailMessage, message.set_content, message.add_alternative, message.get_content_type. The arguments and return values are the central parts of the demonstration.
  3. Determine whether this sample only builds/parses a message or would actually contact a remote service. A constructed message is not a delivered message.

Observed Output


multipart/alternative
['text/plain', 'text/html']

This output was produced by the example in the available local Python test environment. Output that depends on the operating system, time, installed packages, or object representations may differ on your computer.

Important Details and Limitations


Never treat HTML in an email as trusted. Avoid embedding credentials or active scripts.

Can your test be completed without contacting an account or service? Prefer constructing a message or parsing sample bytes locally before integrating with a mail provider.

Try It Yourself


Add a sentence to each version while keeping the meaning consistent.

Before running the modified example, write down what you expect to happen. Test one normal case and one boundary, missing-input, or invalid case when the operation accepts them.

Common Beginner Mistakes


  • Assuming Send a Plain-Text and HTML Multipart Message behaves the same way with missing, malformed, or unsupported inputs as it does in the worked example.
  • Ignoring this requirement: Never treat HTML in an email as trusted.
  • Treating a small illustrative snippet as a production-ready program without testing failures, cleanup, and the target environment.

Key Takeaways


  • A multipart message can offer text and HTML versions so different mail clients can display the same content.
  • set_content defines the plain-text alternative; add_alternative attaches HTML without discarding the text version.
  • To confirm understanding: Add a sentence to each version while keeping the meaning consistent.
Share this resourceSend this page to someone who may find it useful.