Friday, May 22, 2009

Gem to send mails through Gmail

I made this little gem, gmail_sender that I find useful to log things when I'm running some script in my VPS, I use it for things like this:

require 'rubygems'
require 'gmail_sender'

g = GmailSender.new("gmail_account", "password")
begin
#Some interesting script that returns some interesting results in script_results
g.send("dcadenas@gmail.com", "It worked!", script_results.to_yaml)
rescue => e
g.send("dcadenas@gmail.com", "It didn't work :(", "#{e.message}\n#{e.backtrace}")
raise e
end


So all you need to pass to the send method is an email address, the subject and the email content.


Source code is here and you can install it with sudo gem install dcadenas-gmail_sender (remember to add github to your rubygems sources).

Some useful bash functions

I use this bash functions to open vim with splits for each file in which grep or rak find the pattern I'm searching for:

grepvim(){
grep -rl "$1" $2| xargs mvim -o +/"$1"
}

rakvim(){
rak -l "$1" $2| xargs mvim -o +/"$1"
}