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

Is calling `m.save_message()` prior to adding attachment the correct approach to send large size file attachments?

Open nachiketrss opened this issue 3 years ago • 1 comments

Hello

I am exploring this package and tried the following code:

from O365 import Account

credentials = ('client_id', 'client_secret')

account = Account(credentials, auth_flow_type='credentials', tenant_id='my_tenant_id')
if account.authenticate():
    print('Authenticated!')
    mailbox = account.mailbox('sender_email@my_domain.com') 
    m = mailbox.new_message()
    m.to.add('[email protected]')
    m.subject = 'Testing!'
    m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
    m.attachment.add = 'filename.txt'
    m.send()

It works when file size is under 4MB. When the file size is beyond that value it gives HTTP 413 error.

To get around this issue I followed the documentation and tried using the beta protocol as follows:

from O365 import Account, MSGraphProtocol

credentials = ('client_id', 'client_secret')

protocol = MSGraphProtocol(api_version='beta')
account = Account(credentials, auth_flow_type='credentials', tenant_id='my_tenant_id', protocol=protocol)
if account.authenticate():
    print('Authenticated!')
    mailbox = account.mailbox('sender_email@my_domain.com') 
    m = mailbox.new_message()
    m.to.add('[email protected]')
    m.subject = 'Testing!'
    m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
    m.attachment.add = 'filename.txt'
    m.send()

The above is making use of beta protocol as mentioned in the documentation but it doesn't work (return 413 error/same as 1st example) for attachment file size beyond 4MB.

The following solution worked but since it is not documented I want to get some advise on this please:

from O365 import Account

credentials = ('client_id', 'client_secret')

account = Account(credentials, auth_flow_type='credentials', tenant_id='my_tenant_id')
if account.authenticate():
   print('Authenticated!')
   mailbox = account.mailbox('sender_email@my_domain.com') 
   m = mailbox.new_message()
   m.to.add('[email protected]')
   m.subject = 'Testing!'
   m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
   m.save_message()
   m.attachment.add = 'filename.txt'
   m.send()

So there are 2 issues:

  1. Beta protocol isn't able to send mail with large (>4MB) attachments
  2. Adding m.save_message() before attachment.add seems to allow me to send emails with large file size without using beta protocol. I suppose this works by saving the email into drafts and then adding the attachment to it, and send.

Does the documentation need updating please to mention that calling m.save_message() prior to adding attachment is the correct approach to send large size file attachments?

nachiketrss avatar Sep 23 '22 16:09 nachiketrss

Thanks! I’ll update the docs and also clear the error messages

alejcas avatar Sep 23 '22 17:09 alejcas

I am not able to send an attachment with neither alternative (using beta protocol or the usual one). For me 'Message' object has no attribute 'attachment', also authenticate() missing 1 required keyword-only argument: 'scopes'

Could it be related with the version I'm using? I've tried with 2.0.15 and 2.0.0

liberamiguel avatar Sep 28 '22 16:09 liberamiguel

it's attachments.

Better look a little bit into the code: https://github.com/O365/python-o365/blob/master/O365/message.py#L212

alejcas avatar Oct 06 '22 13:10 alejcas

thanks for the tips @nachiketrss, this is so easy, and doesn't need any beta protocol, which in this case becomes useless strange it's not mentionned in microsoft doc

yeyeric avatar Jan 10 '23 09:01 yeyeric

@janscas I believe that this was addressed with PR #1014 ? If you agree then we probably have #337 to close as well.

kwilsonmg avatar Oct 10 '23 17:10 kwilsonmg

@janscas I believe that this was addressed with PR #1014 ? If you agree then we probably have #337 to close as well.

Yes. thanks

alejcas avatar Oct 19 '23 07:10 alejcas