Send Outlook Email Via Python?

user3262424 picture user3262424 · Jun 13, 2011 · Viewed 209.1k times · Source

I am using Outlook 2003.

What is the best way to send email (through Outlook 2003) using Python?

Answer

TheoretiCAL picture TheoretiCAL · Jul 26, 2013
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional

# To attach a file to the email (optional):
attachment  = "Path to the attachment"
mail.Attachments.Add(attachment)

mail.Send()

Will use your local outlook account to send.

Note if you are trying to do something not mentioned above, look at the COM docs properties/methods: https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-object-outlook. In the code above, mail is a MailItem Object.