python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

AttributeError for sending mail with binary attachment

Open R4ccoon5 opened this issue 3 years ago • 1 comments

If I understood correctly, I can attach a binary (in my case of a PDF) to a message as follows: message.attachments.add(attachments=[(bytes, fileName)])

However, I get the following error when trying to send the mail:

  File "/opt/venv/lib/python3.9/site-packages/O365/message.py", line 697, in send
     self.save_draft()
   File "/opt/venv/lib/python3.9/site-packages/O365/message.py", line 953, in save_draft self.attachments._update_attachments_to_cloud()
   File "/opt/venv/lib/python3.9/site-packages/O365/utils/attachment.py", line 478, in _update_attachments_to_cloud file_size = attachment.attachment.stat().st_size
 AttributeError: 'NoneType' object has no attribute 'stat'

I have already looked at the code and cannot understand what exactly I am doing wrong. Is it possible that this is broken or what am I doing wrong?

Thanks in advance

R4ccoon5 avatar Dec 02 '22 15:12 R4ccoon5

The object should be a BytesIO object.

You can add one like this:

attachment = (bytesio_object, 'attachment_name')
message.attachments.add([attachment])

alejcas avatar Dec 12 '22 11:12 alejcas