28 June 2024
How to send Outlook email with Python
> pip install pywin32
import win32com.client
inbox = win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI")
# print(dir(inbox))
inbox = win32com.client.Dispatch("Outlook.Application")
# print(dir(inbox))
mail = inbox.CreateItem(0x0)
mail.To = "testTo@test.com"
mail.CC = "testcc@test.com"
mail.BCC = "testcc@test.com"
mail.Subject = "Send TEST"
mail.Body = "This is the body"
mail.Attachments.Add(u"path to attachment")
mail.Send()
print('Email sent successfully.')
Remember that in the attachment to use escape characters on windows systems i.e.
c:\users\me should be c:\\users\\me
Subscribe to:
Posts (Atom)