1

I have written code to download MS exchange server email body as html using exchangelib library but it has downloaded html content without <> bracket

import datetime
from exchangelib import ServiceAccount, Account, Configuration, DELEGATE
from exchangelib import EWSDateTime, EWSTimeZone, EWSDate, 

server = 'server url'
username = 'username'
password= 'password'
credentials = ServiceAccount(username=username, password=password)
config = Configuration(server=server, credentials=credentials)
account = Account(
    primary_smtp_address='xyz@gmail.com',
    config=config, credentials=credentials,
    autodiscover=False,
    access_type=DELEGATE
)

# to fetch 6 days before emails
tz = EWSTimeZone.localzone()
end = tz.localize(EWSDateTime.combine(EWSDate.today(), datetime.time(0)))
start = end - datetime.timedelta(days=6)
for item in account.inbox.filter(datetime_received__range=(start, end)):
    emailbody = item.body
    with open('test.html', 'w', encoding='utf-8') as fdata:   
        fdata.write(emailbody)

Expected result:

<html><head>....</head></html>

Actual output:

html head .../head /html
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63

1 Answers1

1

It's possible that this is just how the body is received from the server. If you enable debug logging, you can see what the body actually looks like.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63