02 / URLS, NETWORKS, AND EMAIL · LESSON 07

Read email headers as evidence

Parse message bytes, decode the subject, and preserve repeated routing headers.

15 min · learn at your pace

Preserve the message before interpreting it

A raw email contains headers, a blank line, and a body. Header values may continue onto another physical line, and fields such as Received may occur repeatedly. Splitting lines on colons loses that structure. Read the original bytes and let BytesParser(policy=policy.default) build the message object.

An explicit policy gives you the modern email interface and decoded header values. Encoded words can represent Unicode subjects and display names. Use address parsing to separate display labels from mailbox addresses; a familiar display name alone does not establish who sent the message.

Repeated fields carry separate observations

message.get_all("Received", []) retains every occurrence in its stored order. That order is commonly newest first. Preserve it here instead of pretending every timestamp came from a trusted clock. A missing optional header should become an empty value, not a fabricated address.

The From and Reply-To fields describe different destinations. A difference can be worth investigating but also occurs in legitimate mailing systems. This parser does not authenticate the sender or validate SPF, DKIM, or DMARC. Even an Authentication-Results header needs a known trust boundary before you rely on it.

Return a concise dictionary for later analysis while keeping the raw message intact. The checks include folded headers, Unicode and absent fields, so a parser-based solution is more dependable than extracting this fixture with a regular expression.

Try an example.py
from email import policy
from email.parser import BytesParser
from pathlib import Path
message = BytesParser(policy=policy.default).parsebytes(Path("suspicious.eml").read_bytes())
print(message["Subject"])
print(message.get_all("Received", []))
YOUR TURN

Put it into practice.

Define email_headers(raw), accepting bytes. Return subject and message_id as strings (empty if absent), from and reply_to as lists of mailbox address strings without display names, and received as a list of all Received values in stored order. Decode encoded subjects and folded headers using the email library. Preserve address spelling. Do not claim the message is authenticated.

See an explained solution
One way to solve it.py
from email import policy
from email.parser import BytesParser
from email.utils import getaddresses

def email_headers(raw):
    message = BytesParser(policy=policy.default).parsebytes(raw)
    addresses = lambda name: [address for _, address in getaddresses([str(v) for v in message.get_all(name, [])]) if address]
    return {"subject": str(message.get("Subject", "")), "from": addresses("From"), "reply_to": addresses("Reply-To"), "message_id": str(message.get("Message-ID", "")), "received": [str(value) for value in message.get_all("Received", [])]}

The standard parser handles encoded words and folded fields. get_all keeps repeated routing evidence, and the returned dictionary makes no authentication claim.