Mail

From Enso Wiki

Jump to: navigation, search

[edit] Ruby

This extension makes it easy to mail selected text or files to people in your Outlook address book. Reads global contact list to build a list of bound strings.

I've only tested it on my machine that runs Outlook 2007, and it works fine. However Outlook is notoriously known to display security messages whenever its automation is used. If this is the case, check the Ruby on Windows blog for possible remedies.

require "xmlrpc/client"
require "xmlrpc/server"
require 'win32ole'
 
class MailExtension  
  def initialize
    @enso = XMLRPC::Client.new("127.0.0.1", "/", 11374).proxy
  end
  
  def callCommand( commandName, postfix )
	begin
	  selected_text = @enso.getUnicodeSelection
		
	  outlook = WIN32OLE.new('Outlook.Application')
	  message = outlook.CreateItem(0)
		
	  message.To = postfix unless postfix.empty?
	  
	  if !selected_text.empty?
	    if selected_text.length > 65
		  message.Body = selected_text
		else
		  message.Subject = selected_text
		end
	  end
	  
	  @enso.getFileSelection.each { |file| message.Attachments.Add(file, 1) }
		
	  message.Display
	rescue Exception => e
		@enso.displayMessage("<p>Error in mail command</p><caption>#{e.message}</caption>")
	end
	
    return true
  end 
end
 
class EnsoExtensionServer
  
  def initialize()
    @enso = XMLRPC::Client.new("127.0.0.1", "/", 11374).proxy
    register_commands
    server = XMLRPC::Server.new(11375, "127.0.0.1", 4, $stdout, true, true, '')
    server.add_handler("", MailExtension.new)
    begin
      server.serve
    rescue
    ensure
      unregister_commands
    end
  end
  
  def register_commands
    @enso.registerCommand("http://127.0.0.1:11375", 
      "mail {recepient}", 
      "Enso makes outlook better", 
      "<p>This command creates mail message, filling in some of the details for you.</p>",
      "bounded"
    )
    
    set_bound_strings
  end
  
  def unregister_commands
	puts "unregistering commands"
    @enso.unregisterCommand("http://127.0.0.1:11375", "mail {recepient}")
  end
  
  def set_bound_strings
	begin	
	  result = Array.new
	
	  outlook = WIN32OLE.new('Outlook.Application')
      mapi = outlook.GetNameSpace('MAPI')      
      address_list = mapi.Session.AddressLists('Global Address List')      
      address_entries = address_list.AddressEntries
      address_entries.each { |entry| result.push entry.Name.downcase }
	
      @enso.setCommandValidPostfixes("http://127.0.0.1:11375", 
        "mail {recepient}",
        result
      )
    rescue Exception => e
      @enso.displayMessage("<p>Error while setting bound strings in the mail command</p><caption>#{e.message}</caption>")      
	end
 
  end
end
 
EnsoExtensionServer.new