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

Email not sending when attachment is added; attachment not being added

Open TheTrette opened this issue 3 years ago • 4 comments

Thank you for this great lib, it's definitely got everything that I've been looking. I'm having some trouble with adding an attachment to an email. It's probably not an issue, more of user error and not properly understanding the directions. I'm trying to send an email with an XLSX file as an attachment. Everything works great, I can send an email without the attachment, but once I add the attachment the email doesn't get sent. I don't receive an error message either. Here is my code:

from O365 import *
import os

id = '###'
scrt = '###'
tn_id = '###'

credentials = (id, scrt)
protocol = MSGraphProtocol(api_version='beta')
pth = os.getcwd()
token_backend = FileSystemTokenBackend(token_path=pth, token_filename='o365_token.txt')

account = Account(credentials, auth_flow_type='credentials', tenant_id=tn_id, token_backend=token_backend, protocol=protocol)
    #  if account.authenticate():
    #     print('Authenticated!')

mlbx = account.mailbox(resource='[email protected]')
msg = mlbx.new_message()
msg.to.add(to_list)
msg.cc.add(cc_list)
msg.subject = subject
msg.body = body

attachment = '/Directory/SubFolder/FileName.xlsx'
if attachment is not None:
     msg.attachments.add([attachment])
     print(msg.has_attachments)

msg.send()

I receive an output as True for msg.has_attachments. I've tried researching into the Issues log to see if I can determine where I went wrong, I've tried to add msg.save_message() before the attachment line. This didn't provide the solution, it returns a JSONDecode Error message.

Forgot to add: attachment is a file path that equals something like: '/Directory/SubFolder/FileName.xlsx' I'm using the current version of O365 Python 3.7.9 I'm on a MacOS

I appreciate the help! Thank you!

TheTrette avatar Jul 28 '21 14:07 TheTrette

I have kinda similar problem, but the error I receive is requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/me/sendMail | Error Message: Cannot send this mail due to a transient error. Please try again later (0x00004000)., WASCL UserAction verdict is not None. Actual verdict is TransientError.

DaniEzzeddine avatar Aug 05 '21 18:08 DaniEzzeddine

Hi

@TheTrette The current state of the library on MSGraph protocol does not allow to send files bigger than 4MB. Is this file bigger than that?

@DaniEzzeddine Seems like a MS Graph temporary error

alejcas avatar Sep 03 '21 09:09 alejcas

@janscas No, these files are typically at most 1MB, 90% of the time they are smaller.

TheTrette avatar Sep 10 '21 19:09 TheTrette

@TheTrette It should work... Can you check if the string location works ?

Like try to Path(attachment).open('r') that path and see if this actually leads to the file.

If it does then the attachment should be attached

alejcas avatar Sep 20 '21 12:09 alejcas