I am new to getting emails in Python except for using things for ArcGIS. However, I have been assigned a task to continuly look at an email address for an incoming email with a paticular subject and to extract just a few things from that email. I think I can do that pretty easily. However, I am using Exchangelib for Python and when I pull emails, and generate the text I get a whole bunch of HTML code with it. It's on all emails that I pull from Python. Is there a way to use something like BeautifulSoup to do this? If so how?
from exchangelib import DELEGATE, Account, Credentials
from bs4 import BeautifulSoup
credentials = Credentials(
username='user.name@company.com', #Microsoft Office 365 requires you to use user.name@domain for username
password='MyS3cretP@$$w0rd' #Others requires DOMAIN\User.Name
)
account = Account(
primary_smtp_address='primary.email@company.com',
credentials=credentials,
autodiscover=True,
access_type=DELEGATE
)
# Print first <number selected> inbox messages in reverse order
for item in account.inbox.all().order_by('-datetime_received')[:1]:
print(item.subject, item.body)
I am also attaching two images. One of what the email looks like, and the other what python is spitting out.
Again, what I want to learn how to do is to get that where what python is spitting out is to be plain text.
UPDATE: This was just a test email to show you all the HTML that is being generated with Exchangelib. Eventually, emails will look something like this
Outage Request Number: 1-001111
Outage Request Status: Completed
Status Updated By: Plant
Requested Equipment: Hose
Planned Start: Outage: 01/01/2000 01:00
Planned End: Outage: 01/01/2000 02:00
Actual Start: 01/01/2000 01:00
Actual Completion: 01/01/2000 02:00
Duration: Exactly 1.00 Hour(s)
Continuous
Outage Request Priority: Forced
Request Updated: 01/01/2000 00:01