easy +10 pts

Parse email header

Extract the sender, recipient, and subject from a raw email header string.

Write a function `parse_email_header(header: str) -> dict` that takes a raw email header as a string and returns a dictionary with keys `'from'`, `'to'`, and `'subject'`. The header uses standard email header format: each line is `Field: value` with the field name followed by a colon and a space. The fields are case-insensitive (e.g., 'From:', 'FROM:', 'from:' are all valid). The string may contain extra whitespace around the value and may have trailing newlines. You may assume every header contains exactly one 'From:', one 'To:', and one 'Subject:' field. Lines that are not one of these three fields should be ignored. Return the values trimmed of leading and trailing whitespace. The dictionary keys must be exactly 'from', 'to', 'subject' (lowercase).

Constraints

The input `header` will be a non-empty string. The total length will not exceed 10,000 characters. There will be exactly one 'From:', 'To:', and 'Subject:' field (case-insensitive). The values may contain spaces but do not contain newlines.

Example

>>> parse_email_header('From: alice@example.com\nTo: bob@example.com\nSubject: Hello')
{'from': 'alice@example.com', 'to': 'bob@example.com', 'subject': 'Hello'}
>>> parse_email_header('SUBJECT: Meeting\nFROM: jane@x.io\nTO: team@x.io\nDate: today')
{'from': 'jane@x.io', 'to': 'team@x.io', 'subject': 'Meeting'}
10 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Split the header into lines using the newline character '\n'.
For each line, split on the first colon to separate the field name from the value.
Compare field names case-insensitively using .lower() and store the trimmed value in the result.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.