0
votes

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()   
2
Why don't you just use the SMTP library? - Swift
is it possible to send outlook email with it? it uses webbrowser to do it? becouse i have outlook.com blocked in my browser for company reasons. - Rexilife
Pretty sure it just sends and receives requests. You can setup the server addresses yourself. Do you know the email credentials? All you need is the email server address, the protocols it uses and the username/password and you can send without need of the outlook client itself - Swift
It doesnt use a web browser, it is all do as a script and only uses python. - Swift
If it's just like a hotmail account, I'm pretty certain you'll have no issues :) - Swift

2 Answers

1
votes

You never actually attach a file - add a line like

newMail.Attachments.Add(attachment1)
0
votes

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