Python – Send HTML-formatted email via Outlook 2007/2010 and win32com

emailhtmloutlookpython

Is there a way to send HTML-formatted email using Python's win32com.client (which utilizes Outlook 2007/2010). The format I'm using now looks like this:

import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "the subject"
newMail.Body = "body text"
newMail.To = "recipient@example.com"
attachment1 = "c:\\mypic.jpg"
newMail.Attachments.Add(attachment1)
newMail.Send()

This will send an email using Outlook, sent from the currently authenticated user, to the specified recipient, with a subject, content, and attached image.

I want to be able to send an inline image, which can be achieved using an "Embedded" attachment, or simply to link to and image using HTML, or embed an image using HTML and a Base64-encoded image.

HTML is my preferred approach, but any HTML I add to the body is formatted and encoded as plain text (e.g. < becomes &lt;). Is there a way to tell Outlook the body content is HTML and should be parsed as such?

Best Answer

This is the way to make the body in html format

newMail.HTMLBody  = htmltext
Related Topic