1

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='aldoushicks@recirclerecycling.com', 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="bspks@lunet.lboro.ac.uk"
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")
Taazar
  • 1,545
  • 18
  • 27
  • This is really weird. Please post the code you are running so we can get a better understanding. Also, please [turn on debug logging](https://github.com/ecederstrand/exchangelib#troubleshooting) to find out the response XML that caused this problem. – Erik Cederstrand Nov 19 '18 at 10:16
  • Thank you Erik, the code is below (forgive my lack of experience ;) and I will turn on logging too and try later: – Patrick Stacey Nov 28 '18 at 15:15
  • If you want the email address of the sender, just access the property: `item.sender.email_address`. I have cleaned up your code a bit to show you how to do it. – Erik Cederstrand Nov 29 '18 at 15:41
  • awesome, how kind. Thanks very much Erik. Sorry not had time to provide the logging yet. Best. P – Patrick Stacey Nov 30 '18 at 16:15

0 Answers0