Rexilife
Rexilife

Reputation: 685

What is the correct syntax to send attachment in outlook email with python?

I'm trying to send an attachment with this Python code to send emails via Outlook.

The code at the moment looks like this and it works to send text msg, but my Eexcel file is not attaching.

import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "test"
newMail.Body = "123"
newMail.To = "[email protected]"
attachment1 = "C:/Users/myuser/Desktop/aaa.xls"
newMail.Send()   

Upvotes: 0

Views: 623

Answers (2)

0m3r
0m3r

Reputation: 12499

Also Attachment Path Use r so the string is treated as a raw string.

Example

attachment1 = r"C:/Users/myuser/Desktop/aaa.xls"
newMail.Attachments.Add(attachment1)

https://docs.python.org/3/reference/lexical_analysis.html#string-literals

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

You never actually attach a file - add a line like

newMail.Attachments.Add(attachment1)

Upvotes: 1

Related Questions