Apr 4, 2008

How to send mail via Gmail by Ruby.

How to send mail via Gmail by Ruby.
Gmail use TLS that isn't supported by Ruby 1.8.
You can use tlsmail.


# gem install tlsmail


A sample code.

require 'tlsmail'
require 'time'

content = <<EOF
From: from@gmail.com
To: to@gmail.com
Subject: Hello!
Date: #{Time.now.rfc2822}

How are you.
EOF

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', 'from@gmail.com', 'password', :login) do |smtp|
smtp.send_message(content, 'from@gmail.com', 'to@gmail.com')
end

This code don't work in Ruby 1.8.3.

10 comments:

Scott said...

This post helped me to successfully use Ruby to send email through Gmail.

Domo arigato gozaimasu!

Kumi said...

I've gone through so many sites that had help for sending email via gmail smtp, but yours was the only one that actually helped! Thanks very much!

Anonymous said...

Awesome! The only thing to add as usual is

require 'rubygems'

You saved me time. Thanks

Ram

Anonymous said...

yeap !.

Very thanks dude..

It helped me a lot..

Great job..

Thanks buddy

Anonymous said...

I'm very grateful for this easy code but ... is there a straightforward way of adding an attachment?
Thx,
Colin

meswerty said...

Thank you very much ;-)

You're the best :P

House 9 said...

I needed to send email from a ruby script, but outside of the rails environment

Worked great, Thanks!

Anonymous said...

this is so awesome.

thank you.

Anonymous said...

Thank you so much, tried so many different solutions and here your's worked perfectly. Gd Bless you.

Anonymous said...

This is really useful, THANK YOU.