Thursday, July 14, 2011

Embedding images in email through outlook

A great resource for all your Ruby on Windows needs is this guy.


http://rubyonwindows.blogspot.com/


Most of the voodoo going on below can be further explained at that site, but I will cover the specific parts I couldn't find there. 


 
require 'win32ole'
email = "someone@anyplace.net"
image1 = "/home/user/images/image1.jpg"
image2 = "/home/user/images/image2.jpg"
outlook = WIN32OLE.new('Outlook.Application')
message = outlook.CreateItem(0)
message.To = email
message.Subject = "My Subject"
# In order to use the images in your message you first have to attach them
message.attachments.add(image1)
message.attachments.add(image2)
# You then have set the MIME type of the attachment, and give it a Content ID
1.upto(message.attachments.count) do |index|
  message.attachments(index).PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001E", "image/jpeg")
  message.attachments(index).PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", message.attachments(index).filename)
end
# This hides the attachments from view
message.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true)
message.HTMLBody = <<-HTML
# Reference each image by the name you gave it as a content ID.
<img src="cid:#{image1}" />
Fill in with delicious HTML
<img src="cid:#{image2}" />
HTML
message.save # Saves a copy in your draft box. Use .send to push that baby out!

No comments:

Post a Comment