Wednesday, 14 September 2011

Send a mail with ruby1.8, ruby1.9 and jruby

I lost the best part of this morning to send a mail with ruby with the constrain it should work with ruby1.8, ruby1.9 and jruby.

This is my best effort:
#!/usr/bin/env ruby
#ENCODING: UTF-8

require 'rubygems'
require 'net/smtp'
require 'mail'

TO = 'you@test.com'
FROM = 'me@test.com'

mail = Mail.new do
  from    FROM
  to      TO
  subject 'This is a test email'
  body    "Test\n\nNow is the time #{Time.now}\n"
end

Net::SMTP.start('localhost') do |smtp|
  smtp.send_message(mail.to_s, TO, FROM)
end

I hope it could save someone's time!

No comments: