Exchangelib item.sender returns bytes

1.2k Views Asked by At

I'm having an issue recently with some values returned from item.sender - attached is a screen dump

Image

As you can see in the lower portion of the graphic, it's not the usual form returned by item.sender which usually is of form:

Mailbox(name='ReCircle Recycling', email_address='[email protected]', routing_type='SMTP', mailbox_type='OneOff')

Has anyone else seen this?

How do you deal with it?

i.e. even though I am using try/except clause, this result still causes my IDE to freeze.

I re-ran the script today using exactly the same date filter and it didn't happen. So I’m forced to ask, what did happen? Why is it not occurring again?

Its weird behaviour. It could jam up a script in future so wondering how to prevent it.

Code:

from collections import defaultdict
from datetime import datetime
import logging

from exchangelib import DELEGATE, Account, Credentials, \
EWSDateTime, EWSTimeZone, Configuration
from exchangelib.util import PrettyXmlHandler

logging.basicConfig(level=logging.DEBUG, handlers=[PrettyXmlHandler()])

gusername="" #deleted :/
gpassword="" #deleted :/
gprimary_smtp_address="[email protected]"
em_dict = defaultdict(list)

def contactServer(pusername, ppassword,pprimary_smtp_address):
    creds = Credentials(pusername, ppassword)
    config = Configuration(server='outlook.office365.com/EWS/Exchange.asmx', \
                           credentials=creds)
    return Account(
        primary_smtp_address=pprimary_smtp_address,
        autodiscover=False, 
        config = config,
        access_type=DELEGATE
    )

print ("connecting to server\n")
account = contactServer(gusername, gpassword, gprimary_smtp_address)

dt_string = "2018-11-01 12:41:19+00:00" #usually comes out from db, stringified for debugging
dt = datetime.strptime(dt_string, '%Y-%m-%d %H:%M:%S+00:00')
tz = EWSTimeZone.timezone('Europe/London') 
last_datetime = tz.localize(dt)

for item in account.inbox.filter(datetime_received__gt=last_datetime):
    if isinstance(item, Message):

        em_dict["username"].append(gusername)
        em_dict["gprimary_smtp_address"].append(gprimary_smtp_address)
        em_dict["datetime_received"].append(item.datetime_received)
        em_dict["subject"].append(item.subject)
        em_dict["body"].append(item.body)
        em_dict["item_id"].append(item.item_id)
        em_dict["sender"].append(item.sender)

        # extract the email address from item.sender
        emailaddr = item.sender.email_address
        print ("debug email addr: ", emailaddr)

print ("downloaded emails\n")
0

There are 0 best solutions below