──────────────────────────────────────

51. Prepare Reports to Share · Prepare an Offline Email Draft

Make a local email message with a subject, text and an attachment for someone to review.

Lesson files · lessons/share-reports/make-an-email-draft

Before you start
- Read back a piece of text to check what was saved.
- Give a file a fixed name when packing it for another reader.

By the end
- Build an offline draft with a subject, a plain-text body and a named text attachment.
- Read the subject, body and attachment filename back to check the draft.

Where you can use it
Prepare a weekly update draft that an organiser can open and review with its attachment before sending.

EmailMessage lets you build a message in memory. From, To and Subject are headers. set_content() sets the body text. add_attachment() adds a file by name. For a byte payload, give it a media type such as application/zip, using maintype="application" and subtype="zip". Use fictional addresses ending in .invalid for this exercise. A message can be saved as a local .eml file with as_bytes() and reviewed before any delivery.

Predict
Which part would a reviewer see in the attachment list?
- checklist.txt
- The room is ready.

Worked example (example.py)

from email.message import EmailMessage

draft = EmailMessage()
draft["From"] = "club@example.invalid"
draft["To"] = "reviewer@example.invalid"
draft["Subject"] = "Room check"
draft.set_content("The room is ready.")
draft.add_attachment("Projector tested.", filename="checklist.txt")

print(draft["Subject"])
for attachment in draft.iter_attachments():
    print(attachment.get_filename())


Example result
Room check
checklist.txt

Read a one-line update and use it as the body. Use club@example.invalid as the sender, reviewer@example.invalid as the recipient and Club update as the subject. Attach the same update as a text file called update.txt. Print the subject, then the draft’s plain-text body, then the name of the attachment, each on its own line.

Starter program (starter.py)

from email.message import EmailMessage

update = input()
draft = EmailMessage()
draft["From"] = "club@example.invalid"
draft["To"] = "reviewer@example.invalid"
draft["Subject"] = "Club update"
draft.set_content(update)
print(draft["Subject"])


Starter input
We finished the map.

Hint
After adding the attachment, use draft.get_body(preferencelist=("plain",)).get_content().rstrip("\n") to read the body. Loop over draft.iter_attachments() to read the filename.

Case 1. Try the sample
Input
We finished the map.
Expected output
Club update
We finished the map.
update.txt

Case 2. Accented text in the body
Input
Café signs are ready.
Expected output
Club update
Café signs are ready.
update.txt

Case 3. Punctuation in the body
Input
Robotics & Design: ready!
Expected output
Club update
Robotics & Design: ready!
update.txt

Stretch
If you are working in a local folder, add a with open("club-update.eml", "xb") block and write draft.as_bytes() to that file. The x creates a new file and protects an existing draft. Review the file locally.

Reflect
What would you check about the address and attached information before handing a real draft to an organiser?

Check your prediction
checklist.txt iter_attachments() gives the attached parts separately from the main body.
