domenicqatest
domenicqatest

Reputation: 1

O365 Send email attachment in Python

I'm trying to send an email attachment using O365. I can't find any documentation on how to do so. Here is my code below:

from O365 import Message

authenticiation = (SENDER_EMAIL, SENDER_PASSWORD)

m = Message(auth=authenticiation)

m.setRecipients(RECIPIENT_EMAIL)

m.setSubject("TEST")

m.setBody("TEST")

m.sendMessage()

This successfully sends the email message, but how does one attach an attachment using 'from O365 import Message'?

Upvotes: 0

Views: 6030

Answers (1)

Milton
Milton

Reputation: 958

Please try this example (make sure you are importing all modules of O365):

from O365 import *

        authentication = ('[email protected]','password')
        m = Message(auth=authentication)

        m.setRecipients('[email protected]')
        m.setSubject('Email with attachment')
        m.setBody('This email has an attachment.')

        att = Attachment(path='myCSVFile.csv')
        m.attachments.append(att)

        m.sendMessage()

I hope this example is useful for you.

Upvotes: 5

Related Questions