python-o365
python-o365 copied to clipboard
Is calling `m.save_message()` prior to adding attachment the correct approach to send large size file attachments?
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:
- Beta protocol isn't able to send mail with large (>4MB) attachments
- Adding
m.save_message()beforeattachment.addseems to allow me to send emails with large file size without usingbetaprotocol. 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?
Thanks! I’ll update the docs and also clear the error messages
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
it's attachments.
Better look a little bit into the code: https://github.com/O365/python-o365/blob/master/O365/message.py#L212
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
@janscas I believe that this was addressed with PR #1014 ? If you agree then we probably have #337 to close as well.
@janscas I believe that this was addressed with PR #1014 ? If you agree then we probably have #337 to close as well.
Yes. thanks