Thursday, June 22, 2006

How to use GMail SMTP server to send emails in Rails ActionMailer

Recently I had to use Ruby to send emails. I don't want to go through the hassle of setting up a mail server on my build box, and since I use GMail's hosted webmail service I am thinking I might be able to use their smtp mail server to do it.

Rail's ActionMailer was simply the automatic choice, since I am building a rails app.

Turns out GMail supports only SSL SMTP mailing service, meaning if you cannot create a SSL connection to its SMTP server, you cannot send email through them. My Rails and Ruby (1.84) version do not yet support creating a SSL SMTP connection through Net::SMTP. DHH writes about how to do so through installing msmtp here, but we developers just obviously love options =)

The dynamic nature of Ruby allows me to enhance the functionality of that class. I found the following code from a couple Japanese posts here and here on the web (fairly low Google ranking). Pasting them in my /vendor/plugins as follow:


$ cat vendor/plugins/action_mailer_tls/init.rb
require_dependency 'smtp_tls'


$ cat vendor/plugins/action_mailer_tls/lib/smtp_tls.rb
require "openssl"
require "net/smtp"

Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret

sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output

check_response(critical { recv_response() })
do_helo(helodomain)

raise 'openssl library not installed' unless defined?(OpenSSL)
starttls
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.sync_close = true
ssl.connect
@socket = Net::InternetMessageIO.new(ssl)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output
do_helo(helodomain)

authenticate user, secret, authtype if user
@started = true
ensure
unless @started
# authentication failed, cancel connection.
@socket.close if not @started and @socket and not @socket.closed?
@socket = nil
end
end

def do_helo(helodomain)
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue Net::ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
end

def starttls
getok('STARTTLS')
end

def quit
begin
getok('QUIT')
rescue EOFError
end
end
end

So now, in your ActionMailer::Base's server settings if you have:

ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mycompany.com",
:authentication => :plain,
:user_name => "username",
:password => "password"
}
You call your ActionMailer::Base's deliver method (perhaps from a custom subclass), it will send an email through GMail. Mission accomplished.

76 comments:

Uri Lewin said...

Thanks for this extremely useful post, I was just looking myself for a handy solution for sending emails from my Rails development environment.

I followed the instructions and I'm getting the following 'wrong number of arguments' error [test controller method 'create' is the one that generates the message]:


ArgumentError (wrong number of arguments (1 for 2)):
/vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:11:in `initialize'
/vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:11:in `new'
/vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:11:in `do_start'
c:/ruby/lib/ruby/1.8/net/smtp.rb:378:in `start'
c:/ruby/lib/ruby/1.8/net/smtp.rb:316:in `start'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.2.1/lib/action_mailer/base.rb:447:in `perform_delivery_smtp'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.2.1/lib/action_mailer/base.rb:333:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.2.1/lib/action_mailer/base.rb:333:in `deliver!'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.2.1/lib/action_mailer/base.rb:258:in `deliver'
/app/controllers/test_controller.rb:62:in `create'

Are you familiar with this situation?

Stephen Chu said...

I have not experienced your situation. But I would check the following:
1. What is your ActionMailer version?
2. Is hardware/software firewall an issue? I was developing on a Mac.
3. Try this in server/console mode. Create the TCPSocket instance, and pass it into the offending line. If you are able to create the socket, that means your server_settings are okay.

This looks to me like you are using different version of something than I do.

Uri Lewin said...

Stephen, hello again,

After further digging I found that the problem was related to compatibility with Ruby 1.8.2 Net/Protocol and Net/SMTP files.

After replacing them with the latest version (taken manually from ruby 1.8.4) it works like a charm.

Thanks again.

Uri

Stephen Chu said...

I am happy that my blog can help =) Thanks for coming.

Anonymous said...

Thank you so much. I have been trying to figure out this for a couple days. Works like a magic.

retto said...

Visitor says;
Thanks for helpfully explanation. I do the step by step and it is working nice. Really I was very happied when I saw my stupid mail in my mail account.

Anonymous said...

ok, I've followed this all the way through and I can send an email by using UserNotifier.deliver_signup_notification from the script/console. But when I run the same code as part of an Observer (UserObserver), the method after_create is called and the code executes (at least the log shows everything happening), but I never recieve the email. As I can send manually with the script/console, all my settings must be correct, so any idea why when I call it as an observer callback it runs the code but fails to deliever?

Simon said...

Hmmm... Have tried this and the ssl.connect fails with an SSLError. Are there any gotchas with SSL?

Philbo said...

Dude, I love you!! I can't tell you how long I searched for this, and it works beautifully. Thank you very, very much.

Anonymous said...

Thank you so very much! Worked perfectly :)

Michael Zlatkovsky said...

I'm new to ruby, so I had some problems implementing your code. First of all, do I need to create smtp_tls.rb, or is it supposed to already exist? If so, in which lib (in main folder or in vendor\ruby\actionmailer) do I put it in? Also, where exactly do I put the ActionMailer::Base.server_settings configurations? I fooled around with the code for an hour, but still could not get it to work. Is there any sample file online that uses this TLS SMTP implementation? I'd appreciate any help on the matter.

Thanks,
- Michael Zlatkovsky

PS: I'm not sure if Blogger will notify me if you respond to my question, so if you could email me the answer at michael.zlat@gmail.com, that would be great. Thanks again.

Jason Liebe said...

Yes, Michael you will have to create those directories in files within vendor/plugin and copy in the code show. ActionMailer confi goes in environment.rb.

This is an uber helpful post. I've spent a few hours trying to get actionmailer to work w/ gmail account until I came across this post. Works now, thanks.

Muhammad Hassan said...

Thanks for help,
I am just having a "Random" error when i send mail in my rails application which is:
"535 5.7.1 Credentials Rejected"
this error appears and disappears randomly (with out changing the password).
any ideas

Wes Oldenbeuving said...

Thank you for providing this plugin!

Alberto Morales said...

Love it! Just what I was looking for.

Thanks for posting.

Anonymous said...

Hi, I'm new to rails so sorry if I missed anything really apparent, but this isn't working for me.

I created the files, pasted the code,

added the following to environment.rb:
ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com",
:port => 465,
:domain => "gmail.com",
:authentication => :plain,
:user_name => "[my email]",
:password => "[my password]"
}

created the following method in a mailer:
def forgot_password(to, pass, sent_at = Time.now)
@subject = "Your password is ..."
@body['pass']=pass
@recipients = to
@from = 'support@yourdomain.com'
@sent_on = sent_at
@headers = {}
end

and made a valid template

then ran the following in a unit test:
assert Notifications.deliver_forgot_password("[another email]", "Blah")

For some reason it returns true but does not send the e-mail... any help would be appreciated
-George

caton said...

George,

if, in your test code, you have:

ActionMailer::Base.delivery_method = :test

it will set the delivery method to test mode so that email will not actually be delivered

jintha's-eye said...

I have followed the steps in this tutorial, but when i restart my server(Webrick), it gives me the following error:

no such file to load -- openssl (MissingSourceFile)

I am new rails. Can you please help me.

jintha's-eye said...

I kind of figured my problem and by doing
apt-get install libopenssl-ruby

It now works. Thanks for this tutorial.

Anonymous said...

Thanks for this! Works great. One note I might add for others, I could only get it to work by using username@domain.com as the user_name.

Kleas said...

Hi Stephen,
thanks for this great plugin.
I am really struglin to get it work on winXP, Ruby1.8.5, rails egde (log http://pastebin.com/872716)
and..
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => :plain,
:user_name => "user",
:password => "passws"
}

any clues?
many thanks!
Spyros

ndoherty13 said...

What cost me an hour or so was not restarting the web server after editing environment.rb ... stupid of me, I know. Hopefully someone else can learn from my mistake.

And thanks a mil for this info, Stephen. Much appreciated.

Anonymous said...

Is there a way to change ActionMail::serversetting while the server is running?
I want my web to be able to send mail from 3-4 different domains using differnt port and authentication.

Thanks in advanced

Wilhelm said...

And even after months, this post has helped. Thanks! Funny thing is, this post ranks pretty low on google. I had to run through a couple different websites to get here, eventually finding it on the rails wiki.

And thanks for telling me to restart the web server...I actually hadn't even thought of that. thanks!

Anonymous said...

Help!

So, the developers of our website must have found your post and was able to successfully use our Google Domains mail account to send from Rails ActionMailer. BUT, now, we actually want to use another SMTP, because of the limitations of changing the sender info. So, my question is, how do you remove this so that other smtps will work? I'm not too good at Rails, but I know the structure.

I have been at this for days!

Thanks.

yikes said...

I have setup as in your webpage. Everything seems to be fine. However, I receive no email. I am using Mongrel_rails. Part of the log is:

-> "220 mx.google.com ESMTP z20sm739285pod\r\n"
<- "EHLO gmail.com\r\n"
-> "250-mx.google.com at your service, [218.250.209.246]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-STARTTLS\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "STARTTLS\r\n"
-> "220 2.0.0 Ready to start TLS\r\n"
<- "EHLO gmail.com\r\n"
-> "250-mx.google.com at your service, [218.250.209.246]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-AUTH LOGIN PLAIN\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "AUTH PLAIN AG5pY2hvbGFzLmxhaQBuaWNrbmljaw==\r\n"
-> "235 2.7.0 Accepted\r\n"
<- "MAIL FROM:<>\r\n"

I am learning ruby only for some time. Please help...

yikes said...

Sorry! I forget to mention 1 thing. I am trying to add the Auth type to my app which is supposed to send me email for login.

Roberto R said...

For some reason, it worked with port 587 but not with port 465 for Google Apps mail.

Jimmy Z said...

Thank you! Thank you!

This is going to make my life so much easier!

Works like a charm.

beam said...

This is super! I spent like 8 hours before finding your post. I did as you said, and first second attempt worked for me!

I had to replace the line

ActionMailer::Base.server_settings = {

with

config.action_mailer.server_settings = {

And the magic happened! Thanks so much!

Anonymous said...

Works like a charm right out of the box!

Daniel said...

This is so cool. My company just switch our mail server to Gmail webapps and that was just what I needed. Thanks for sharing!

hakita said...

Hi, i got the following problem using ar_mail on a fedora core 4.
Any idea on what to do ?

<- "EHLO www.google.com\r\n"
-> "250-mx.google.com at your service, [*.*.*.*]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-STARTTLS\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "STARTTLS\r\n"
-> "220 2.0.0 Ready to start TLS\r\n"
<- "EHLO www.google.com\r\n"
p-> "250-mx.google.com at your service, [*.*.*.*]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-AUTH LOGIN PLAIN\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "AUTH PLAIN dsffddfdfdfdsfdsfdfdsfdsfdsf\r\n"
-> "235 2.7.0 Accepted\r\n"
<- "QUIT\r\n"
Unhandled exception SSL_read:: wrong version number(OpenSSL::SSL::SSLError):
/usr/lib/ruby/1.8/net/protocol.rb:133:in `sysread'
/usr/lib/ruby/1.8/net/protocol.rb:133:in `rbuf_fill'
/usr/lib/ruby/1.8/timeout.rb:56:in `timeout'
/usr/lib/ruby/1.8/timeout.rb:76:in `timeout'
/usr/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
/usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
/usr/lib/ruby/1.8/net/protocol.rb:126:in `readline'
/usr/lib/ruby/1.8/net/smtp.rb:664:in `recv_response'
/usr/lib/ruby/1.8/net/smtp.rb:651:in `getok'
/usr/lib/ruby/1.8/net/smtp.rb:686:in `critical'
/usr/lib/ruby/1.8/net/smtp.rb:649:in `getok'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/smtp_tls.rb:99:in `quit'
/usr/lib/ruby/1.8/net/smtp.rb:426:in `do_finish'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/smtp_tls.rb:32:in `start'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/smtp_tls.rb:19:in `start'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/action_mailer/ar_sendmail.rb:362:in `deliver'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/action_mailer/ar_sendmail.rb:443:in `run'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/action_mailer/ar_sendmail.rb:440:in `loop'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/action_mailer/ar_sendmail.rb:440:in `run'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/lib/action_mailer/ar_sendmail.rb:311:in `run'
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.2.0/bin/ar_sendmail:5
/usr/bin/ar_sendmail:18:in `load'
/usr/bin/ar_sendmail:18

shagreel said...

I have been using this solution for quite a while now and it has been working great! Thank you.

Recently though I have been having the same problem as hakita. For me, the mail is actually going through but the exception is thrown after that.

-> "220 mx.google.com ESMTP n40sm161197wag\r\n"
<- "EHLO familylearn.com\r\n"
-> "250-mx.google.com at your service, [166.70.233.12]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-STARTTLS\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "STARTTLS\r\n"
-> "220 2.0.0 Ready to start TLS\r\n"
<- "EHLO example.com\r\n"
-> "250-mx.google.com at your service, [166.70.233.12]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-AUTH LOGIN PLAIN\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "AUTH PLAIN ycyyt4ee4876c876v8r8MGQlSDAj\r\n"
-> "235 2.7.0 Accepted\r\n"
<- "MAIL FROM: example@example.com\r\n"
-> "250 2.1.0 OK\r\n"
<- "RCPT TO:user@example.com\r\n"
-> "250 2.1.5 OK\r\n"
<- "DATA\r\n"
-> "354 Go ahead\r\n"
writing message from String
wrote 546 bytes
-> "250 2.0.0 OK 1184712018 n4787g61197wag\r\n"
<- "QUIT\r\n"

Unhandled exception SSL_read:: wrong version number(OpenSSL::SSL::SSLError):
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/protocol.rb:133:in `sysread'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/protocol.rb:133:in `rbuf_fill'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/timeout.rb:56:in `timeout'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/timeout.rb:76:in `timeout'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/protocol.rb:126:in `readline'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:664:in `recv_response'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:651:in `getok'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:686:in `critical'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:649:in `getok'
/vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:61:in `quit'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:426:in `do_finish'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:381:in `start'
/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/1.8/net/smtp.rb:316:in `start'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:567:in `perform_delivery_smtp'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:453:in `__send__'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:453:in `deliver!'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:336:in `method_missing'

Anonymous said...

Me too same error
A OpenSSL::SSL::SSLError ...

SSL_read:: wrong version number
/usr/lib/ruby/1.8/net/protocol.rb:133:in `sysread'

Someone can help??

Shaun said...

Same error here too

OpenSSL::SSL::SSLError (SSL_read:: wrong version number):
/opt/csw/lib/ruby/1.8/net/protocol.rb:133:in `sysread' .........

What could have happened to all of us??

BrianC said...

Same error here too. I needed a quick fix so went back to sendmail for now. I'm assuming google changed something in GMail that broke us all. But I'm not sure what.

Marston A, SugarStats said...

Same errors here too :-(

If anyone has a fix could you post it here?

Joe Noon said...

this is a workaround/fix. im not sure of the implications:

in #quit

- rescue EOFError
+ rescue

adambates said...

I'm also experiencing the SSL Read error and am searching to see if anyone has discovered what's changed.

As did one of the prior posters, I've implemented the following workaround:

Add the lines:
rescue OpenSSL::SSL::SSLError => e
puts('Unexpected Exception: ' + e.message + ' : ' + e.backtrace.join("\n") )

right after the "rescue EOFError" line in the "quit" action of smtp_tls.rb. This will prevent the exception from getting thrown up to your application but will also allow you to monitor the problem.

Eager to hear if anyone has been able to discover the root of the problem.

btw, to the person that discovered and made the ssl add-on available - thank you so much. It's been working like a charm for quite some time now.

Joe Noon said...

@adambates: are you using this in a rails app? i could see the usefulness of the "puts" if you are doing something from command line or script/runner, but where does the puts go when you are running mongrel, etc?

FYI my speculation on the issue is that gmail's smtp server is closing the connection prematurely. Anyway, as far as I know QUIT is a "nice" thing, but not a required thing, so Im not sure I care about what problems #quit encounters at all.

I guess what I am wondering is... since mail is still making it out ok, why get notified about this?

Anonymous said...

Thks Adambates

Your workaround is working fine for me

Anonymous said...

Another workaround to suppress the error is to add

ActionMailer::Base.raise_delivery_errors = false

to your environment.rb file

Bryan said...

Just another thank you, worked like a charm! Spent a couple hours trying to get sendmail to work, just to finally come to the conclusion that my ISP must be blocking the port, meaning I'd have to use an SMTP server.

Two great things about Ruby community: always hackers solving problems, and always troubleshooters posting solutions!

stakadush said...

i tried your solution, but didnt manage to make it work... i found using the tlsmail gem easier...

install the tlsmail gem:

sudo gem install tlsmail

then in your config/environment.rb file add this:

require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

ActionMailer::Base.server_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'yourdomain.com',
:authentication => :plain,
:user_name => 'username',
:password => 'password'
}

Anonymous said...

Great, thanks for guide.
Worked for me.
Cheers

Philippe said...

Great Tip and big time saver. Got my gmail mailer working in 3 minutes!

JTigger said...

Hey "stakadush", I also found tlsmail, but was looking for set-up instructions. Thanks for posting!

Belzebooth said...

Hi everybody. I must mention that I am a newbie developer in Ruby. I did everything in the tutorial, but I can't seem to get it working.

My settings in environment.rb are:
ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "localhost.localdomain",
:authentication => :plain,
:user_name => "myusername@gmail.com",
:password => "mypassword"
}

ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"

And the output that I receive when I try to send an email:
-> "250-mx.google.com at your service, [x.x.x.x]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-STARTTLS\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "STARTTLS\r\n"
-> "220 2.0.0 Ready to start TLS\r\n"
<- "EHLO localhost.localdomain\r\n"
-> "250-mx.google.com at your service, [x.x.x.x]\r\n"
-> "250-SIZE 28311552\r\n"
-> "250-8BITMIME\r\n"
-> "250-AUTH LOGIN PLAIN\r\n"
-> "250 ENHANCEDSTATUSCODES\r\n"
<- "AUTH PLAIN xxxxxxxxxxxxxxxxxxx==\r\n"
-> "235 2.7.0 Accepted\r\n"
<- "MAIL FROM:<>\r\n"
-> "250 2.1.0 OK\r\n"
<- "RCPT TO:<>\r\n"
-> "555 5.5.2 Syntax error w7sm10461046mue\r\n"
<- "QUIT\r\n"
-> "221 2.0.0 mx.google.com closing connection w7sm10461046mue\r\n"

I know I am missing something ... if someone could point me in the right direction...

I also tried stakadush's solution, but I get the same error.

Thanks!

John said...

Hey Stephen, Just to say thanks for this but I keep getting the error

execution expired

Have you any suggestions?

Tyler said...

Sick post...thanks alot. The original post on google with the link to plugin is down, I just copy/pasted your code and it worked.

One note with google, username should be user@gmail.com, or, if you have google apps email installed for your own domain, user@yourdomain.com.

Now my website activation emails won't hit the spam bin.

Thanks!

George Bailey said...

Wow! This worked perfectly for me the first time. And stuff like this *never* works the first time for me :-)

I'm not using it with gmail, but rather my hosting service. All I did was create the two files, restarted my application, and waited for the error so I could figure out what went wrong. To my amazement it just worked! That just doesn't happen.

Thanks a million for this info!

quynh said...

Hi guys, I got this to work but the email that is being sent out has the "from" field set to my google username even though in my email template I set the "from" field to be something else. Does anyone know a way to get around this and have it not display the "from" field with the google username?

Anonymous said...

stephen chu i love you. great post. saved my life.

Sandy said...

Guyz, I am trying to send multiple mails so i have to authenticate multiple times which consumes a hell lotta time, can anyone suggest a way.

findchris said...

I second quynh's post:
>>>
Hi guys, I got this to work but the email that is being sent out has the "from" field set to my google username even though in my email template I set the "from" field to be something else. Does anyone know a way to get around this and have it not display the "from" field with the google username?
>>>

Thanks.

quynh said...

Sandy, I found a link that has a solution for mass mailing. I haven't tried this myself but hopefully this will help you.

quynh said...

Oops, forgot the link. http://www.myowndb.com/blog/?p=20

Anonymous said...

I third the post about the mysterious from field. Actionmailer looks to be using the correct from address if you check in the logs. So it must either be something with the tls_mail plugin or an issue with gmail itself. Has anyone tested tls_mail with anything else? Thanks!

Anonymous said...

Outstanding example. Worked the first time for me. I've seen others having some problems, and offered suggestions. It all adds up to good stuff. Keep it going, and Thanks!

Pari said...

Worked fine for me too!!

Is there any limit on the number of mails that can be sent in one go through gmail server?

Actually, i want to use it for mass mailing..

TIA,
Priya Saini

paul said...

Awsome, thanks a lot

Anonymous said...

only problem is when you send over 500.

http://www.google.com/support/a/bin/answer.py?answer=59797&query=limit&topic=&type=

David Kelso said...

You saved me many hours. Thankyou!

krister said...

Yes, this works! Very nice. Thanks for this.

ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :plain,
:user_name => "xxx@gmail.com",
:password => "xxx"
}

those area the settings that worked for me.

brett said...

I just noticed

check_auth_args user, secret, authtype if user or secret

needs to be changed to

check_auth_args user, secret if user or secret

for Ruby 1.8.7

Javi said...

Thanks man, it took me hours to get to your post. It finally got it working!!!

Eivind said...

Works like a charm! Thanks!

srihari said...

I am getting the following error when using the smtp_tls

:/ruby/lib/ruby/1.8/timeout.rb:60:in `rbuf_fill': execution expired (Timeout::Error)
from c:/ruby/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from c:/ruby/lib/ruby/1.8/net/smtp.rb:663:in `recv_response'
from c:/ruby/lib/ruby/1.8/smtp_tls.rb:15:in `do_start'
from c:/ruby/lib/ruby/1.8/net/smtp.rb:685:in `critical'
from c:/ruby/lib/ruby/1.8/smtp_tls.rb:15:in `do_start'
from c:/ruby/lib/ruby/1.8/net/smtp.rb:377:in `start'
from c:/ruby/lib/ruby/site_ruby/1.8/action_mailer/base.rb:681:in `perform_delivery_smtp'
from c:/ruby/lib/ruby/site_ruby/1.8/action_mailer/base.rb:523:in `__send__'
from c:/ruby/lib/ruby/site_ruby/1.8/action_mailer/base.rb:523:in `deliver!'
from c:/ruby/lib/ruby/site_ruby/1.8/action_mailer/base.rb:395:in `method_missing'

Anonymous said...

Anyone get this error?

530 5.7.0 Must issue a STARTTLS command first. l31sm1276848rvb.39

srihari said...

Can you explain more about this command "STARTTLS". How to use this in the code.

Currently i am using Windows and Ruby lastest version

Anonymous said...

stakadush --> thanks man, you rock!

el_toro said...

Another thanks to stakadush, worked like a charm when all other tutorials failed. Cheers!

Anonymous said...

Brilliant! Works a treat.

jebastin said...

hello mr stephen,

we are sent a bulk of message using send blaster,then not going to invalid email id.

how to collect invalid email id?
but it is waste of one in that database.

how to collect demain failure email contects in same page?

any software are there?

pls help me.

by jeba,karunya

J Wells said...

Six years later and this post is STILL useful! Thank you much. We've got a production server that is running rails 1.2 and the instructions here worked like a charm.

The only thing I'd add is that I wanted to send as one of our group addresses in GMail for Business so that if the customer replied the proper list of folks would see it. However, groups don't have login credentials so you can't send as the group. I'm using my account to send, so I went into my GMail settings as setup "Send as" for the group address and now the delivered emails have the proper group address as the "from" address instead of my personal address.

Win!