3
votes

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:[email protected]" 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:[email protected]">

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
                     );
3

3 Answers

2
votes
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

1
votes

In python you must define before the property PR_ATTACH_CONTENT_ID_W because it's not defined in pywin32 mapitags (win32com.mapi.mapitags)

from win32com.mapi.mapitags import PROP_TAG, PT_UNICODE 

# defines the property PR_ATTACH_CONTENT_ID_W  not defined  in    pywin32 mapitags.
# number of PR_ATTACH_CONTENT_ID_W Property is in MSDN Library
# https://msdn.microsoft.com/en-us/library/cc765868.aspx
# all mapi tags are defined in https://msdn.microsoft.com/en-us/library/cc815492%28v=office.15%29.aspx
mapitags.PR_ATTACH_CONTENT_ID_W = PROP_TAG(PT_UNICODE, 0x3712)

# now you can use the property PR_ATTACH_CONTENT_ID_W like Dmitry explained you.
-1
votes

Take one HTML page, with your text content and CSS

Grab an image you want to send embedded in your email

Use a Base64 encoder to turn your binary photo into a huge text string (like: http://www.greywyvern.com/code/php/binary2base64)

Replace your normal image source with that string

Save your file and send as normal