1

Although I've been happily running this script for best part of a year, i recently upgraded to Catalina OSX and reinstalled Exchangelib. Now I get an error with item_id:

'Message' object has no attribute 'item_id'

Here's my code, I would love to know what I'm doing wrong please TIA ps-forgive any convoluted coding...

from exchangelib import DELEGATE, Account, Credentials, Message, \
EWSDateTime, EWSTimeZone, Configuration
from exchangelib.util import PrettyXmlHandler
import logging
logging.basicConfig(level=logging.DEBUG, handlers=[PrettyXmlHandler()]) 
from datetime import datetime, timedelta
import monthdelta as md
import sqlite3 
import pandas as pd
import pm_ews_module as pem 

__DBPATH__ = "/Users/patrickstacey/CODE/JUMPY_CODE/dev/data/test_data_tbase11_002"    
__CONFIGFILE__ =  '/Users/patrickstacey/CODE/JUMPY_CODE/dev/config/jumpyConfig.csv' 
__OUTLOOK_EMAIL__ = 'bspks@lunet.lboro.ac.uk'
_PRIMARY_SMTP_ADDRESS_ = 'bspks@lunet.lboro.ac.uk'
__OUTLOOK_PASSWORD__ = '****'

def connect_to_EWS(__OUTLOOK_EMAIL__, __OUTLOOK_PASSWORD__, _PRIMARY_SMTP_ADDRESS_): 
    creds = Credentials(__OUTLOOK_EMAIL__,__OUTLOOK_PASSWORD__)
    config = Configuration(server='outlook.office365.com/EWS/Exchange.asmx', \
                           credentials=creds)

    return Account(
    primary_smtp_address=_PRIMARY_SMTP_ADDRESS_,
    autodiscover=False, 
    config = config,
    access_type=DELEGATE
    )

last_analysis = pem.determine_start_date_required(__OUTLOOK_EMAIL__) 

if last_analysis == "no records": 
    df = pd.read_csv(__CONFIGFILE__)
    retrodays = df['detail'].where(df['item'] == "demo_user_days_retro").dropna().values
    retrodays = int(retrodays)
    last_analysis = None
    last_analysis = datetime.today() - timedelta(days=retrodays)
    (year,month,day,hour,mins,secs) = pem.unpackDateElements(str(last_analysis))
    tz = EWSTimeZone.timezone('Europe/London')
    last_analysis = tz.localize(EWSDateTime(year, month, day, hour, mins, secs))

account = connect_to_EWS(__OUTLOOK_EMAIL__, __OUTLOOK_PASSWORD__, __OUTLOOK_EMAIL__)

for item in account.inbox.filter(datetime_received__gt=last_analysis):
    if type(item) == Message:
        try:   
            db = sqlite3.connect(__DBPATH__)
            cursor = db.cursor()
            cursor.execute("INSERT INTO escores_log(email, datetime, subject, body, emailtype, pos_threshold, item_id, status, sender) VALUES(?,?,?,?,?,?,?,?,?)", (__OUTLOOK_EMAIL__, str(item.datetime_received), pem.deEmojify(item.subject), item.text_body, "received", 0.5, item.item_id, 0, item.sender.email_address))
            print("Inserted an email from ",item.sender.email_address," about ",item.subject," on ",str(item.datetime_received))
            db.commit()
            db.close()

        except Exception as e: 
            print ("Exception found: "+str(e))
            pass

2 Answers2

1

item_id was renamed to id in version 1.12.0, and finally deprecated in 2.0.0. See notes in the CHANGELOG: https://github.com/ecederstrand/exchangelib/blob/master/CHANGELOG.md#200

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

looks as though item_id is now called id. I took an educated guess. so the script works again. look fwd to hearing any other views on this. with thanks.