pythonapiattachmentoutlook-web-app

python o365 API. Cant add attachment


Im facing trouble with adding attachment to messages i create and send via O365 python API\

Documentation for the API - https://o365.github.io/python-o365/latest/api.html

I tried so many things, but nothing works.....

from O365 import Account

#We create a OWA mailbox instance
class Mailbox:
    def __init__(self):
        #Email credentials
        emailscopes = ['xxx', 'yyy', 'zzz', 'kkk', 'lll']
        emailcreds = ('XXXXXXXXXXXX', 'YYYYYYYYYYYYYYYYYYYYYY')

        #Login to the O365 account
        self.email_account = Account(emailcreds, auth_flow_type='authorization', scopes=emailscopes)
        #Get the mailbox (holds all the folders)
        self.mailbox = self.email_account.mailbox()
        #save the inbox folder in a variable
        inbox_main = self.mailbox.get_folder(folder_name="Inbox")
        self.inbox = inbox_main.get_folder(folder_name="Ronica")



    def send_mail(self, mail, HTML):
        print("mail")
        recipients = mail.to
        m = mail.reply(to_all=False)
        m.to.clear(); m.bcc.clear()
        m.to.add(self.get_recipients(recipients))
        m.body = HTML
        ##################################################
        #### NEED TO ADD A .docx AS AN ATTACHMENT HERE####
        ##################################################
        m.send()

The code is very long, thus i only added the important part. I first create instances of: Account, mailbox, inbox, and another folder.

Then the function send_mail() recives: the Message instance we want to reply on + HTML code. Everything works fine, but I'm unable to attach a file to the message im creating.... Past answers don't seem to work as well as....

If any one knows how to attach a .docx file to a message i want to send, with the API i will be thankful.


Solution

  • Found the solution.
    m.attachment.add()