Reputation: 17
Since has_keys()
function is removed in Python 3 and above, I don't know what code to use to check if a "From: " header exist in an email file.
Here is my current snippet:
with open(emlFile, 'rb') as fp:
headers = BytesParser(policy=policy.default).parse(fp)
if 'From:' in headers:
str = headers['from']
I used in
instead of has_keys('from')
since that is what I saw from other threads in StackOverflow
Upvotes: 0
Views: 623
Reputation: 23223
Quoting official Python docs (emphasis mine):
Read all the data from the binary file-like object fp, parse the resulting bytes, and return the message object. fp must support both the readline() and the read() methods on file-like objects.
Since result of parse is Message
instance, let's check that class docs:
__contains__(name)
:
Return true if the message object has a field named name. Matching is done case-insensitively and name should not include the trailing colon. Used for the in operator, e.g.:
if 'message-id' in myMessage:
print('Message-ID:', myMessage['message-id'])
Your check includes trailing colon and is invalid according to docs. Valid check is:
if 'from' in headers:
do_something()
Upvotes: 1