Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - 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 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 = "[email protected]"
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?

like image 464
Jason Avatar asked Jul 09 '14 09:07

Jason


1 Answers

This is the way to make the body in html format

newMail.HTMLBody  = htmltext
like image 75
Yegers Avatar answered Nov 16 '22 03:11

Yegers