You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug/email-non-ascii-addresses: sender and recipient names come out RFC 2047-encoded, non-ASCII addresses fail the message #4542
partition_email() formats the sent_from, sent_to, cc_recipient and bcc_recipient metadata with email.utils.formataddr(). Since the message is parsed with email.policy.default, the headers are already decoded, and formataddr() then:
writes any non-ASCII display name as an RFC 2047 encoded-word, so the metadata has =?utf-8?b?5byg5Lyf?= <[email protected]> instead of 张伟 <[email protected]>. Every mail client writes non-ASCII names this way on the wire, so this affects most mail with an accented or non-Latin sender or recipient name. The subject of the same message is decoded correctly;
raises UnicodeEncodeError on a non-ASCII address, which RFC 6532 (SMTPUTF8) allows, e.g. jü[email protected]. This fails the whole message, not just the metadata field.
To Reproduce
importemail.policyimportiofromemail.messageimportEmailMessagefromunstructured.partition.emailimportpartition_email# 1. non-ASCII display names, RFC 2047-encoded on the wire as mail clients domsg=EmailMessage(policy=email.policy.SMTP)
msg["From"] ="José García <[email protected]>"msg["To"] ="张伟 <[email protected]>"msg["Subject"] ="Grüße"msg.set_content("Hello")
element=partition_email(file=io.BytesIO(msg.as_bytes()))[0]
print(element.metadata.sent_from, element.metadata.sent_to, element.metadata.subject)
# ['=?utf-8?b?Sm9zw6kgR2FyY8OtYQ==?= <[email protected]>'] ['=?utf-8?b?5byg5Lyf?= <[email protected]>'] Grüße# 2. a non-ASCII address (RFC 6532)msg=EmailMessage(policy=email.policy.SMTPUTF8)
msg["From"] ="José <josé@ejemplo.es>"msg["To"] ="[email protected]"msg.set_content("Hello")
partition_email(file=io.BytesIO(msg.as_bytes()))
# UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 3: ordinal not in range(128)# File "unstructured/partition/email.py", line 187, in from_address# formatted_addrs = [email.utils.formataddr(addr) for addr in addrs]
Expected behavior
The names and addresses as written: ['José García <[email protected]>'], ['张伟 <[email protected]>'] and ['José <josé@ejemplo.es>']. ASCII names and addresses should keep their current format, including the quoting of names like "Doe, John" <[email protected]>.
Environment Info
scripts/collect_env.py needs pkg_resources, which isn't installed here, so by hand: main at e2f22b3 (0.27.26), Python 3.12.13, Linux on WSL2 (glibc 2.43).
Additional context
I found the address crash with tests/mails/mail_test_12 from SpamScope/mail-parser, whose To: header has a raw 8-bit address. A small helper that quotes and escapes the display name the way formataddr() does, without the encoding step, fixes both. I have it with tests and will open a PR.
Activity
fix(email): keep non-ASCII sender and recipient names readable (#4543)