lib init
This commit is contained in:
58
mail_intake/parsers/headers.py
Normal file
58
mail_intake/parsers/headers.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from typing import Dict, List, Tuple, Optional
|
||||
|
||||
|
||||
def parse_headers(raw_headers: List[Dict[str, str]]) -> Dict[str, str]:
|
||||
"""
|
||||
Convert a list of Gmail-style headers into a normalized dict.
|
||||
|
||||
Input:
|
||||
[
|
||||
{"name": "From", "value": "John Doe <john@example.com>"},
|
||||
{"name": "Subject", "value": "Re: Interview Update"},
|
||||
...
|
||||
]
|
||||
|
||||
Output:
|
||||
{
|
||||
"from": "...",
|
||||
"subject": "...",
|
||||
...
|
||||
}
|
||||
"""
|
||||
headers: Dict[str, str] = {}
|
||||
|
||||
for header in raw_headers or []:
|
||||
name = header.get("name")
|
||||
value = header.get("value")
|
||||
|
||||
if not name or value is None:
|
||||
continue
|
||||
|
||||
headers[name.lower()] = value.strip()
|
||||
|
||||
return headers
|
||||
|
||||
|
||||
def extract_sender(headers: Dict[str, str]) -> Tuple[str, Optional[str]]:
|
||||
"""
|
||||
Extract sender email and optional display name from headers.
|
||||
|
||||
Returns:
|
||||
(email, name)
|
||||
|
||||
If name cannot be determined, name will be None.
|
||||
"""
|
||||
from_header = headers.get("from")
|
||||
if not from_header:
|
||||
return "", None
|
||||
|
||||
# Common forms:
|
||||
# Name <email@domain>
|
||||
# email@domain
|
||||
if "<" in from_header and ">" in from_header:
|
||||
name_part, email_part = from_header.split("<", 1)
|
||||
email = email_part.rstrip(">").strip()
|
||||
name = name_part.strip().strip('"') or None
|
||||
return email, name
|
||||
|
||||
return from_header.strip(), None
|
||||
Reference in New Issue
Block a user