I made reference to the How can I get an email message's text content using Python?
The issue with the way I have implemented is that the output is not what I am trying to achieve. I want to get the content from an email. I made use of get_payload and other examples. Even made use of regex (which isnt the best option for many files).
I am manually inserting the file through listOfFiles[0] which will eventually loop through the listOfFiles list.
The code:
from email.message import EmailMessage
sender = ''
reciever = ''
body = ''
with open(listOfFiles[0]) as file:
msg = EmailMessage()
msg.set_content(file.read())
for part in msg.walk():
if part.get_content_type() == 'text/plain':
print(part.get_payload(decode=True))
I also tried to implement it this way which resulted in the same way
if msg.is_multipart():
content = ''.join(part.get_payload(decode=True) for part in msg.get_payload())
else:
content = msg.get_payload(decode=True)
Results: Results from using the above implementation
Expected Outcome
loan servicing-jessica weeber 800-393-5626 jweeber@spbank.com