Python – How to send HTML format email with emebbed images(not attachment ) using Outlook 2010(no smtp) and python

htmloutlookpython

Just as shown below, currently I have already implemented sending HTML format email using Outlook 2010 and python

def send_mail_via_com(self):
  o = win32com.client.Dispatch("Outlook.Application")
  Msg = o.CreateItem(0)
  Msg.To = self.myEmailAddress 
  Msg.BCC = self.myEmailAddress

  Msg.Subject = 'This is Subject'
  Msg.HTMLBody  = htmlEmailContent
  Msg.Send()

Going forward, I want to show some embed images in the HTML format email, I've check the the HTML source, it looks like following:

 <img border=0 width=117 height=20 id="Picture_x0020_2" src="cid:image001.png@01CE9386.AB9B5E70" alt="Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: cid:786FB7C2-A5F6-462A-B0FD-EA865CD02221@xxx.example.com">

I tried to attached the image files(using the same name referenced in above html codes as attachment) but it will just be attachment and won't be shown in the text body as embed image. Our mail serve doesn't support SMTP protocol, so I cannot use the smtplib as that in Sending Multipart html emails which contain embedded images. Can somebody help me out? thanks in advance!

    attachment1 = "G:\\image001.png"
    Msg.Attachments.Add(attachment1)

2013/8/8 Update, I got a C# version codes via http://social.msdn.microsoft.com/Forums/vstudio/en-US/6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64/how-to-embed-image-in-html-body-in-c-into-outlook-mail, could anybody show me Python counterpart codes for these:

   Attachment attachment = newMail.Attachments.Add(
                      @"E:\Pictures\image001.jpg"
                      , OlAttachmentType.olEmbeddeditem
                      , null
                      , "Some image display name"
                     );

   string imageCid = "image001.jpg@123";

   attachment.PropertyAccessor.SetProperty(
                     "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
                     , imageCid
                     );

    newMail.HTMLBody = String.Format(
                      "<body><img src=\"cid:{0}\"></body>"
                     , imageCid
                     );

Best Answer

import win32com.client

o = win32com.client.Dispatch("Outlook.Application")
newMail = o.CreateItem(0)

attachment = newMail.Attachments.Add("E:\Pictures\image001.jpg", win32com.client.constants.olEmbeddeditem, 0, "Some image display name")
imageCid = "image001.jpg@123"
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid)
newMail.HTMLBody = "<body><img src=\"cid:{0}\"></body>".format(imageCid)

msg.Send()

https://msdn.microsoft.com/en-us/library/office/ff869553(v=office.15).aspx

Related Topic