Getting AttributeError: sender while iterating to get specific values

104 Views Asked by At

Getting "AttributeError: sender" is thrown as the exchange query iterates. Same with other values (message_id, etc) too. My only option at this point is to put a try/catch around it and need to refactor a lot of content under the loop. However, I would think the query should not be crashing under normal circumstances due to any data issue. Please let me know what could be going wrong. There appears to be a 'bad' email object that causes it?

kwargs = {"is_read": False}
kwargs["datetime_received__gt"] = some_date_time

filtered_items = my_exchange._service_account.inbox.filter(**kwargs)
filtered_items.page_size = 20

print(filtered_items.count())

3     <-- 3 objects

for sender_obj, msg_id, msg_subj, msg_text, msg_size in filtered_items.values_list("sender", "message_id", "subject", "text_body", "size").iterator():
    print(sender_obj)
    count = count + 1
    print(count)


Mailbox(name='Some User1', email_address='[email protected]', routing_type='SMTP', mailbox_type='Mailbox')
1
Mailbox(name='Some User2', email_address='[email protected]', routing_type='SMTP', mailbox_type='OneOff')
2

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/exchangelib/queryset.py", line 273, in __iter__
    yield from self._format_items(items=self._query(), return_format=self.return_format)
  File "/usr/local/lib/python3.6/site-packages/exchangelib/queryset.py", line 352, in _item_yielder
    yield item_func(i)
  File "/usr/local/lib/python3.6/site-packages/exchangelib/queryset.py", line 380, in <lambda>
    item_func=lambda i: tuple(f.get_value(i) for f in self.only_fields),
  File "/usr/local/lib/python3.6/site-packages/exchangelib/queryset.py", line 380, in <genexpr>
    item_func=lambda i: tuple(f.get_value(i) for f in self.only_fields),
  File "/usr/local/lib/python3.6/site-packages/exchangelib/fields.py", line 189, in get_value
    return getattr(item, self.field.name)
AttributeError: sender
1

There are 1 best solutions below

4
On BEST ANSWER

It looks like you are trying to get the sender field of something that is not a Message. Probably your inbox contains a meeting request or some other non-message object.

I'm not sure this is a bug. What did you expect to be the result of getting the sender attribute of something that does not have a sender field?

If you want only Message objects in your list, you can try adding a filter on item_class='IPF.Note'.