Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to send a message through Outlook with Ruby?

My work requires me to automate e-mail generation for certain tests. I've been looking around but havent been able to find a reasonable solution that can be implemented quickly. It needs to be in outlook and not some other mail server as we have some strange authentication rules in place, and we need the option of saving drafts instead of just sending the message.

Apparently win32ole can do this, but I can't find any reasonably simple examples.

like image 233
Andrew Harrington Avatar asked Sep 26 '12 00:09

Andrew Harrington


1 Answers

Assuming that the Outlook credentials are stored and you are set to autologin to Outlook, WIN32OLE does the trick quite nicely:

require 'win32ole'
outlook = WIN32OLE.new('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = "Hey look a subject!"
message.Body = "Yes this is dog"
message.Recipients.Add '[email protected]'
message.Recipients.Add '[email protected]'
message.Attachments.Add('C:\Path\To\File.txt')
#Want to save as a draft?
message.Save
#Want to send instead?
message.Send

This is in fact quite well documented in "Automating Outlook with Ruby: Saving Mail Messages To Files", as is automating the rest of windows with Ruby.

You may have an authorization issue, which, if it appears, can be solved using "Advanced Security for Outlook".

like image 152
Tadgh Avatar answered Oct 03 '22 23:10

Tadgh