Tuesday, October 11, 2011

Improved version of mailing from command line using ruby


After I wrote my first mail program, I was not happy with sticking in the recepient's email addr and message in the program. So I modified the program to take recepient's address and the message from command line. The message is terminated by entering a new line with just period (.).
#The following program has been tested with Ruby1.9.1
# Used my Gmail Acct. If not SMTP settings need to change
require 'net/smtp'

puts "Enter the recepient's email id";
toaddr = gets.chomp
puts "Enter message (To terminate enter a new line with .)"
msg = "";
line = gets
until line.chomp.eql? "."
 msg << line;
 line = gets
end

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

smtp.start(Socket.gethostname,'username','password',:login) do |server|
   smtp.send_message msg, 'fromaddr',toaddr
end

Have fun using it.

No comments:

Post a Comment