pythonhtmlemailoutlook

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


Is there a way to send HTML-formatted email using Python's win32com.client (which uses 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 from the currently authenticated user to the specified recipient, with a subject, content, and attached image. I want to send an inline image which can be achieved using an "Embedded" attachment, or link to an image using HTML or embed a Base64-encoded image.

HTML is preferred, 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 HTML should be parsed as such?


Solution

  • This is the way to make the body in html format

    newMail.HTMLBody  = htmltext