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!

Creating Windows Pop ups

Sometimes it's the simple things that make you go in circles. Sometimes you need a dang pop up!

Took a bit of searching but I finally found this page after looking for how to do the equivalent of "wscript.echo".

http://tinyurl.com/6zk9pvs

Quick code dive

require 'win32ole'

wsh = WIN32OLE.new("wscript.shell")
wsh.popup("Hello World!")

That's pretty much it to emulate wscript.echo.

We want POWER!

Our wsh.popup method gives us 4 fields to customize.

wsh.popup({message in the body},{time for window to stay open before} ,{title of the window},{window type} + {type of icon})

Link to summary of window/button types: http://ss64.com/vb/popup.html

#script to tell us if our burrito's are done. Please don't use this code. It's for your own good.


require 'win32ole'

wsh = WIN32OLE.new("wscript.shell")

if Burrito.instance.burning? #singleton Burritos, the best!
wsh.popup("YOUR BURRITOS ARE ON FIRE!",0,"DANGER HUNGRY MAN!", 0 + 48) 

Consider yourself informed. Now go turn off your microwave! 

Win32OLE in Ruby

I work at a Microsoft shop and like all IT people, I often find myself having to script some task. Often it's usually scripting something in the office suite, or creating a service. I'm going to document my fumbles in the Windows world. I hope you enjoy it.