Message353983
It is actually parsed correctly and serialized back when you try to convert it to a string representation:
from email.parser import BytesFeedParser
import email.policy
def main():
eml_string = 'From: Nobody <""@example.org>'
parser = BytesFeedParser(policy = email.policy.default)
parser.feed(eml_string.encode())
msg = parser.close()
print(msg.get('From').addresses[0].addr_spec)
print(repr(msg.get('From')._parse_tree))
print(msg.as_string())
Running this gives me:
@example.org
AddressList([Address([Mailbox([NameAddr([DisplayName([Atom([ValueTerminal('Nobody'), CFWSList([WhiteSpaceTerminal(' ')])])]), AngleAddr([ValueTerminal('<'), AddrSpec([LocalPart([QuotedString([BareQuotedString([ValueTerminal('')])])]), ValueTerminal('@'), Domain([DotAtom([DotAtomText([ValueTerminal('example'), ValueTerminal('.'), ValueTerminal('org')])])])]), ValueTerminal('>')])])])])])
From: Nobody <""@example.org>
Notice the : AddrSpec([LocalPart([QuotedString([BareQuotedString([ValueTerminal('')])])])
print() converts the addr-spec into a string, which omits the quotes. This is true for any non-none string too:
hello@example.org
AddressList([Address([Mailbox([NameAddr([DisplayName([Atom([ValueTerminal('Nobody'), CFWSList([WhiteSpaceTerminal(' ')])])]), AngleAddr([ValueTerminal('<'), AddrSpec([LocalPart([QuotedString([BareQuotedString([ValueTerminal('hello')])])]), ValueTerminal('@'), Domain([DotAtom([DotAtomText([ValueTerminal('example'), ValueTerminal('.'), ValueTerminal('org')])])])]), ValueTerminal('>')])])])])])
From: Nobody <"hello"@example.org>
If you prefer the string representation of the header's parsed value, you can try:
print(msg.get('From').fold(policy=email.policy.default))
Which prints:
From: Nobody <""@example.org> |
|
Date |
User |
Action |
Args |
2019-10-05 01:18:00 | maxking | set | recipients:
+ maxking, barry, r.david.murray, andreitroiebbc |
2019-10-05 01:18:00 | maxking | set | messageid: <1570238280.35.0.656906236806.issue38232@roundup.psfhosted.org> |
2019-10-05 01:18:00 | maxking | link | issue38232 messages |
2019-10-05 01:18:00 | maxking | create | |
|