Reputation: 14082
Im using the email
module with Python3.7 to parse a raw email.
The value for the To:
field is the string
import email
msg = email.message_from_file(fh)
print(msg.get('To'))
#---> "name a" <[email protected]>,\n "name b"\n\t<[email protected]>
What would be the easiest and fail-proof way to obtain a list of To:
addresses like:
["[email protected]", "[email protected]"]
Upvotes: 0
Views: 379
Reputation: 362836
Emails in Python are stored in a data structure similar to a multidict with case-insensitive keys. Recipient addresses are stored in the named header field "to".
So, the easiest way is to use EmailMessage.get_all()
:
>>> msg.get_all("to")
["[email protected]", "[email protected]"]
This basic usage assumes the email message was parsed correctly in the first place. The output shown in OP's question suggests that they have a problem earlier on (corrupt input data format), because the email from file was not parsed correctly.
Upvotes: 0
Reputation: 142176
You can use email.utils.getaddresses
on msg.get('To')
... eg:
to_list = msg.get('To')
emails = email.utils.getaddresses([to_list])
Will give you (given your example) 2-tuples of:
[('name a', '[email protected]'), ('name b', '[email protected]')]
Upvotes: 1