0

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='someuser1@myemail.acme', routing_type='SMTP', mailbox_type='Mailbox')
1
Mailbox(name='Some User2', email_address='someuser2@myemail.acme', 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
RajSoundar
  • 341
  • 1
  • 2
  • 8

1 Answers1

1

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'.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • Basically like most other django models I use, I was assuming the fields of a given table to be present in all records unless there is any deprecation. I did not expect message_id and sender to be absent. So I had not accounted for such scenario in my implementation. The IPF.Note filter is sounds great. I will use it. However, I wonder whether this should be handled at the exchange query side to be uniform since the standard facility of value_list can break as in my case. Thank you. – RajSoundar May 04 '21 at 00:48
  • 1
    It's a false premise that an exchange folder is backed by a table. It's not, at least not in the sense that all "rows" have the same shape. exchangelib makes this assumption too. It works surprisingly well mostly, but sometimes it fails, like in this case. – Erik Cederstrand May 04 '21 at 16:41
  • 1
    That said, I do think raising AttributeError is a bit weird. Also, it's not trivial to work around as a user when using `values()` or `values_list()`, and `only()` does not behave like this. https://github.com/ecederstrand/exchangelib/commit/57dffb37ef2b4c2d6f1353144faac730ec688baf should fix this for you. – Erik Cederstrand May 04 '21 at 16:48
  • My query to remove non-messages worked only with {..... 'item_class': 'IPM.Note'} not with 'IPF.Note' . (Thought it is a typo (still could be), but then a search for IPF.Note in the exchange lib repo showed up some hits. Cannot find any documentation reference at MSDN too.) If possible kindly add a filter example using 'item_class': 'IPM.Note in the query at https://ecederstrand.github.io/exchangelib/ for anyone needing. – RajSoundar May 08 '21 at 00:02