Tuesday, October 11, 2011

Mail from command line using ruby


I always wanted to send a simple mail, like a one liner, to someone but felt it was inefficient to open the email editor, type in everything. So I started off writing a simple script that I can use from command line to kick off the mail to someone.
First since I started reading about Ruby and its powerfulness, thought it would be best to start the script in Ruby. So, I searched internet to find the solutions.
Here is the first solution I found. The first solution mentioned in that link didnt work for me for the same exact reasons those folks mentioned. I had 1.8.6 version of Ruby. I upgraded it to Ruby 1.9.0. Even then it didnt work. It kept giving :
"mail.rb:2:in `require': no such file to load -- smtp_tls (LoadError)  from mail.rb:2:in `
'"

mail.rb being my test program.
After a bit of fiddling with it, i went back to google to try the search. This time I found another thread where they discussed this a little more. Anyway, the issue is 1.9.0doesnt need require  'smtp_tls' statement.


Here is the complete program : I am assuming gmail as the server


require 'net/smtp'

msgstr = "Some msg"

smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls

smtp.start(Socket.gethostname,'username','password',:login) do |server|
   smtp.send_message msgstr, 'from', 'to'
end



Hope you like the program

No comments:

Post a Comment