title: Including an embedded image in an Outlook HTML email via Perl
body: <p>I need to generate an HTML email with a banner image embedded.  It must go through an Outlook2007 mail client.  I tried to base64encode the image and put it inline (it <em>looked</em> good) but Outlook would not send the email.  I have culled through many different articles (in various programming languages) that have gotten me to this point but it is still not working.  This code creates the email and attaches the image but the image is not displayed.  </p>
  
  <pre><code>use Win32::OLE;
  use Win32::OLE::Const 'Microsoft Outlook';
  
  my $oMailer = new Win32::OLE('Outlook.Application') or
    die "Unable to start an Outlook instance: $!\n";
  my $oEmail = $oMailer-&gt;CreateItem(0) or
    die "Unable to create mail item: $!\n";
  
  $oEmail-&gt;{'To'} = 'me@here.org';
  $oEmail-&gt;{'Subject'} = "Embedded image test";
  $oEmail-&gt;{'BodyFormat'} = olFormatHTML;
  $oEmail-&gt;{'HTMLBody'}   = "&lt;html&gt;&lt;body&gt;&lt;img src=\"cid:banner.jpg\"&gt;&lt;/body&gt;&lt;/html&gt;";
  
  my $attachments = $oEmail-&gt;Attachments();
  my $bannerAttachment = $attachments-&gt;Add('C:/test/banner.jpg', olEmbeddeditem);
  $bannerAttachment-&gt;PropertyAccessor-&gt;SetProperty(
      "http://schemas.microsoft.com/mapi/proptag/0x3712001E", "banner.jpg");
  
  $oEmail-&gt;save();
  </code></pre>
  
  <p>(BTW, I removed all the <code>Win32::OLE-&gt;LastError()</code> checks before posting because none of them failed anyway.)</p>
  
  <p>When adding the attachment, it does not set the attachment Type to olEmbeddeditem (5);  Don't know if this is relevant to the problem.</p>
  
  <p>The SetProperty does not set the value either.  That is supposed to set the Content ID (cid) that is referenced in the img src in the HTML.  I used the below code to GetProperty and it returns an empty string.  </p>
  
  <pre><code>my $CIDvalue = $bannerAttachment-&gt;PropertyAccessor-&gt;GetProperty(
      "http://schemas.microsoft.com/mapi/proptag/0x3712001E");
  print "&gt;&gt;&gt;CIDvalue = $CIDvalue\n";
  </code></pre>
  
  <p>So close I can taste it!</p>