You are viewing an archived version of danielfischer.com

 

How To: Use GMail as your mail server for Rails 2.0!

Published: January 9th, 2008

If you’re trying to set up ActionMailer with Google’s SMTP server, you may be running into some issues. In this post I explain a way to get ActionMailer chugging away with google!

Google provides a set of tools for your domain absolutely free. It’s called Google Apps1. One of my favorite tools to take advantage of is: GMail for your domain. Some reasons to use their mail server:

  1. Reduces load on your server (You no longer need a mailer daemon)
  2. Reduces storage consumption on your server
  3. It’s free
  4. Google is reliable

An Attempt

When most people try to get gmail to work as their mail server with ActionMailer, they try something like this:

  ActionMailer::Base.smtp_settings = {
    :address  => "smtp.gmail.com",
    :port  => 465,
    :user_name  => "do_not_reply@domain.com",
    :password  => "some_password",
    :authentication  => :login
  }

Unfortunately, this doesn’t work as Gmail requires some funky security tunneling to authenticate ( TLS ). However, a plugin that was released by Marc Chung2, SMTP_TLS, solves the issue. This plugin was made for Rails 2.0.

Grab it

Download it to get started:

  cd /your_rails_app/vendor/plugins
  svn export https://openrain.com/opensource/public/rails/plugins/action_mailer_tls action_mailer_tls

For some reason, script/plugin install doesn’t work. I solved the issue by doing a svn export.

smtp_gmail.rb

Great, now add the file smptp_gmail.rb to your config/initializers folder and put the following in it:

  require "smtp_tls"
 
  mailer_config = File.open("#{RAILS_ROOT}/config/mailer.yml")
  mailer_options = YAML.load(mailer_config)
  ActionMailer::Base.smtp_settings = mailer_options

Mailer.yml

In your config folder, create a file named “mailer.yml” and put the required information in it:

  ---
    :address: smtp.gmail.com
    :port: 587
    :user_name: your_gmail_username@yourdomain.com
    :password: your_gmail_username_password
    :authentication: :plain

After that, you should be gold!

Thanks to Marc for his hard work on releasing a very helpful plugin!

  1. Google Apps []
  2. marc [dot] chung [at] openrain [dot] com []

there is also a possibility to do it great with ssmtp. you use gmail as a smarthost then.

gravatar

Brilliant, just the thing I was after.

Out of interest in the gmail_smtp.rb file is there a reason you use:

mailer_config = File.open(”#{RAILS_ROOT}/config/mailer.yml”)
mailer_options = YAML.load(mailer_config)

Instead of using YAML.load_file()?

mailer_options = YAML.load_file(”#{RAILS_ROOT}/config/mailer.yml”)

Just curious.

I’m going to have a play around with this, looks ideal.

gravatar
  • Jim Neath
  • Jan 10th

Hi Daniel,

Are there any limitations on the volume of mails you can send that you know of?

Infact, are there any limitations/gotchas of using google for your outgoing mail?

Martin

gravatar
  • Martin Smith
  • Jan 10th

I should point out that there’s a limit of 500 emails a day. Other than that, it’s a good solution.

gravatar

[...] I hope it’s helpful to you. I at first searched for a set working settings and it turns out port 587 is the working one (for Rails). I did so but hit into mysterious exceptions with [...]

Nice!

gravatar

Thanks for the usage writeup, Daniel. Couple of notes..

(1) I’m pretty sure ‘plugin install’ is failing because of a shared hosting-related SSL issues on the OpenRain end. Very sorry about that… we’ll hopefully be switching hosting providers soon to fix that issue.

(2) We periodically release plugins and other useful tidbits to that same repository. Currently we only really announce those things on my blog.

@Jim
If you’d like to email Marc about any other suggestions you have, I’m sure he’d appreciate contributions :)

gravatar

Slightly OT -

This is pretty much the best blog design I’ve seen. Ever.

gravatar

@Jim, No reason. At the time, it was so I could use ‘pp’ to inspect the values. Use load_file if you’d like.

I’m not sure why ./script/install doesn’t work; perhaps it’s an SSL issue?

BTW, I use piston to manage this plugin.

gravatar

@ Shanti: Thank you very much for your compliment! I really appreciate it. The design is far from perfect (has some cross-browser issues) but I definitely do love it :)

gravatar

[...] Daniel Fischer - Got Fisch? » How To: Use GMail as your mail server for Rails 2.0! (tags: rubyonrails google) [...]

[...] Daniel Fischer - Got Fisch? » How To: Use GMail as your mail server for Rails 2.0! If you’re trying to set up ActionMailer with Google’s SMTP server, you may be running into some issues. In this post I explain a way to get ActionMailer chugging away with google! (tags: gmail rails email rubyonrails google plugin ruby) Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

[...] Daniel Fischer - Got Fisch? » How To: Use GMail as your mail server for Rails 2.0! (tags: rails gmail rubyonrails) [...]

[...] functionality. For testing this out though, I decided to give Google a shot thanks to an article on how to use gmail as your mail serevr for rails. Very easy to implement, although I’m still unsure on the exact limitations. I don’t [...]

[...] ActionMailer TLS - Have you ever gotten annoyed needing to install a local email server to do your testing? With this plugin you can use Gmail for all your outgoing mail. There’s a 500 email limit or so, but for testing purposes it makes it easy to get setup — especially if you’re alternating between multiple computers. Another option would be to set this up to use Dreamhost or something of course. The advantage of using Google though is it’ll save all your outgoing mail as well, so you’ll be able to see exactly what was sent during testing without digging into logs. [...]

I have an issue,I have not tried it yet,becuase this your write is what I have been searching for. This is my question, could it be able to allow for more than one emails.

gravatar
  • Micro
  • Jan 23rd

Micro, yes, you can have multiple e-mail accounts.

gravatar

Thanks for the way. It works nice. Also, you can install it by:
script/plugin install http://svn.nanorails.com/plugins/action_mailer_tls/

By the way, I have one question. With this method app needs some time to connect to gmail. Because of that I have some speed issues when, for example, sending some email with submitting form.
What can I do to overcome that?
Also, is it true about 500 emails? (Seems not enough for me)

gravatar
  • Mike
  • Feb 1st

[...] Daniel Fischer - Got Fisch? » How To: Use GMail as your mail server for Rails 2.0! (tags: gmail rails email rubyonrails google plugin tutorial) « links for 2008-01-29 [...]

I was at the office until 3am last night (this morning?) trying to get a contact form to work on the client’s box. Come to find out this morning we have to use TLS. First go at this tutorial worked. Great stuff!

gravatar

This is a thing of beauty! Works absolutely seamlessly. Thanks a bunch, and keep up the great work on the blog! :)

gravatar
  • Eric
  • Feb 7th

[...] Daniel Fischer - Got Fisch? » How To: Use GMail as your mail server for Rails 2.0! [...]

Mike,

You could spawn a new thread to do all your emailing while you do other stuff in the foreground (just as long as your not trying to touch the database :-)

thread = Thread.new do
Emailer.deliver_email
end

form_submission_stuff…
thread.join

Your situation is unique (like most situations) so you would have to test this to make sure it worked.

On a side note, I was able to get a Rails app to talk to gmail by using the new net/pop library in the latest ruby (by sticking it in lib/net/pop.rb) and using the following code: http://pastie.caboo.se/150527

Enjoy!

gravatar

[...] the procedure is available here, on the blog of Daniel [...]

Daniel,

Good article on this. Do you need to still make a mailer, configure it as usual? This info seems a bit out of context so I am having difficulties understanding it.

gravatar
  • milites
  • Mar 8th

Great article. I had to do an extra step of creating the initializers folder in the config directory (as my app wasn’t created with Rails 2).

gravatar

Had some trouble with this.

Here is my setup http://pastie.textmate.org/private/vkduvapeezbwkgthaxg

No errors pop up, but then no emails are sent either. This with a google apps account. Anyone else deal with this?

gravatar
  • pimpamster
  • Apr 6th

[...] found a lot of this in a post by my new buddy Daniel Fischer over at his blog. However, an important step in doing this for [...]

I’m getting an error in production.

Net::SMTPFatalError (555 5.5.2 Syntax error. z52sm10896455pyg.1
):
/usr/lib/ruby/1.8/net/smtp.rb:680:in `check_response’
/usr/lib/ruby/1.8/net/smtp.rb:653:in `getok’
/usr/lib/ruby/1.8/net/smtp.rb:635:in `rcptto’
/usr/lib/ruby/1.8/net/smtp.rb:546:in `send0′
/usr/lib/ruby/1.8/net/smtp.rb:545:in `each’
/usr/lib/ruby/1.8/net/smtp.rb:545:in `send0′
/usr/lib/ruby/1.8/net/smtp.rb:472:in `sendmail’
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.0.2/lib/action_mailer/base.rb:588:in `perform_delivery_smtp’
/usr/lib/ruby/1.8/net/smtp.rb:379:in `start’
/usr/lib/ruby/1.8/net/smtp.rb:316:in `start’
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.0.2/lib/action_mailer/base.rb:586:in `perform_delivery_smtp’

gravatar
  • Rob
  • Apr 15th

For me the emails appear to get sent when i look at the development logs, but in fact they do not get delivered.

I am using a Google Apps email, my mailer.yml looks like this:


:address: smtp.gmail.com
:port: 587
:domain: mydomain.com.au
:user_name: ad…@mydomain.com.au
:password: mypassword
:authentication: :plain

When i set delivery_errors to true, i get this “timeout” error:

Timeout::Error (execution expired):
/opt/local/lib/ruby/1.8/timeout.rb:54:in `open’
/vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:10:in `do_start’
/opt/local/lib/ruby/1.8/timeout.rb:56:in `timeout’
/opt/local/lib/ruby/1.8/timeout.rb:76:in `timeout’
/vendor/plugins/action_mailer_tls/lib/smtp_tls.rb:10:in `do_start’
/opt/local/lib/ruby/1.8/net/smtp.rb:378:in `start’
/opt/local/lib/ruby/1.8/net/smtp.rb:316:in `start’
/opt/local/lib/ruby/gems/1.8/gems/actionmailer-2.0.2/lib/
action_mailer/base.rb:586:in `perform_delivery_smtp’
/opt/local/lib/ruby/gems/1.8/gems/actionmailer-2.0.2/lib/
action_mailer/base.rb:469:in `__send__’
/opt/local/lib/ruby/gems/1.8/gems/actionmailer-2.0.2/lib/
action_mailer/base.rb:469:in `deliver!’

Anyone know what could be happening???

Many thanks,

PJ.

gravatar
  • PJ
  • Apr 23rd

I am going to try this right now and hopefully it all works out.

gravatar

It works with direct internet connection. But what to do when you’re behind a proxy (timeout when connecting)?

gravatar

Hi, I am using the gmail smtp to send mail from ruby. But it gives the error like,
-> “220 mx.google.com ESMTP 29sm1363328wfg.0\r\n”
“250-mx.google.com at your service, [122.160.71.28]\r\n”
-> “250-SIZE 28311552\r\n”
-> “250-8BITMIME\r\n”
-> “250-STARTTLS\r\n”
-> “250 ENHANCEDSTATUSCODES\r\n”
“220 2.0.0 Ready to start TLS\r\n”
“250-mx.google.com at your service, [122.160.71.28]\r\n”
-> “250-SIZE 28311552\r\n”
-> “250-8BITMIME\r\n”
-> “250-AUTH LOGIN PLAIN\r\n”
-> “250 ENHANCEDSTATUSCODES\r\n”
“535-5.7.1 Username and Password not accepted. Learn more at \r\n”
-> “535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 29sm1363
328wfg.0\r\n”
=> #<TMail::Mail port=# bodyport=#>

……………………………
What should I do to get it work?

gravatar
  • Shishir
  • May 29th

I’m having the same problem as Shishir. The plugin works perfectly for me when I run the app locally, but once deployed to the production environment that’s when I get the “username and password not accepted” errors. Do I need to open up a particular port or something? I’m not sure what’s going on…

gravatar

Same problem as Shishir and Brent, only in production.
Can anybody shed some light please?

gravatar
  • hari
  • Jun 10th

I’m also having the same problem as Shishir. With mine, local works in both Production or Development but neither works on the deployment server.

As this technique appears in the PragProg Advanced Rails Recipes book, I’ve cross posted this issue there.

http://forums.pragprog.com/forums/43/topics/541

Thanks,

Larry

gravatar

I had a problem recently with Gmail actually blocking the e-mail address I was sending with, because it was using “bad domains”. So it was probably flagged as spam or something.

I was testing the settings by sending e-mail to quentin@example.com, so gmail saw example.com and then flagged the account.

Creating a new account to send mail from fixed it for me.

gravatar

Excellent tutorial! Worked like a charm. I was struggling with the gmail issue for a while, so thank you for putting this together (oh and thanks to the guys who made the plugin)!

gravatar
  • Edwin Broni-...
  • Jun 25th

Looks like you can now…
script/plug install http://code.openrain.com/rails/action_mailer_tls

gravatar
  • S. Brent Fau...
  • Jul 8th

[...] información en el blog de Daniel Fischer y en el README del [...]

You might want to check the GMAIL Account from time-to-time, as far as I know GMAIL is tracking the account, and you might be experiencing login failure.

gravatar
  • Julius Franc...
  • Aug 3rd

Hi,

Am trying to send email through gmail in rails. Its working fine. My
issue is @from is not working. I have assigned @from = ‘abcd@gmail.com’
in my emailer.rb and :user_name => “wxyz@gmail.com” in my
environment.rb file. When i receive mail, from id is “wxyz@gmail.com”
not “abcd@gmail.com”. When its not gmail and port is 25 its works fine.

class Emailer < ActionMailer::Base
def sendmail(subject, email, sent_at, name, message)
@from = ‘abcd@gmail.com’
@subject = subject
@recipients = email
@sent_on = sent_at
@body["name"] = name
@body["message"] = message
end
end

ActionMailer::Base.delivery_method = :smtp
require ’smtp_tls’

ActionMailer::Base.smtp_settings = {
:address => “smtp.gmail.com”,
:port => 587,
:domain => “www.google.com”,
:user_name => “wxyz@gmail.com”,
:password => “mypassword”,
:authentication => :plain
}

Any ideas?

gravatar
  • Vipin Vellak...
  • Sep 16th

Thanks it works great!!

gravatar
  • Sanjay
  • Nov 29th

[...] gave me these pages that might have some helpful info: link, link Also, please use code tags when posting code in the future. They make the code stand out and make [...]

[...] innovative strategy for using ActionMailer to farm out email sending with (almost) everybody’s favorite email host, [...]

Those who have been struggling with ActionMailer working fine on local server (development mode) and not working in production mode should consider following link:

http://filiptepper.com/2009/02/11/hint-fixing-smtp_tlsrb-for-ruby-187/

It solved my problem, might be helpful for others too. Basically the problem is that “check_auth_args in Net::SMTP takes different arguments in Ruby 1.8.7 (which I’m using for development) and in Ruby 1.8.6 (staging)”. And the worst thing is that it does not leave an error in the production.log file. To verify that it really was the problem I had to run ActionMailer to send email from script/console where it raised the error of wrong number of arguments…

hope it’s helpful for others!

gravatar
  • Zeeshan
  • Oct 3rd

I’m running ruby 1.8.7 and Rails 2.3.5 and plugin no more necessary.

Just put in config/initializers a new file smtp_gmail.rb, fill in the required info for authentication and it works like a charme. See also Marc Chung here.

Maybe it hleps others!

gravatar
  • mathiasmex
  • Jan 4th

is this awesome??

gravatar
  • ankitha
  • Jul 13th
Enter your comment

Ready. Set. Go.

In terms of the formatting, you're allowed to use markdown, textile, or basic html; it's truly up to you -- what strikes your fancy?

You don't have to worry about your e-mail address being sold to a russian-spam-mafia. I'm only going to use it for my own weird needs; like asking you out for a date on a lonely night of coding.