Using GMail SMTP with ActionMailer
April 27, 2008
There are a number of solutions that have been posted around the net to get Rails’ ActionMailer to talk to GMail, allowing you to send email from your Rails app. From Googling around, it seems that many bloggers have quoted a post from Anatol Pomozov. However, this is now a dead link:
http://blog.pomozov.info/posts/how-to-send-actionmailer-mails-to-gmailcom.html
Stephen Chu talked about how to use GMail’s smtp server back in 2006:
http://www.stephenchu.com/2006/06/how-to-use-gmail-smtp-server-to-send.html
More recently, Preston Lee talks about the Anatol Pomozov approach here:
http://www.prestonlee.com/archives/63
And Daniel Fischer explains how to use the action_mailer_tls plugin – this method is also mentioned in the new Advanced Rails Recipes book:
http://www.danielfischer.com/2008/01/09/how-to-use-gmail-as-your-mail-server-for-rails/
However, there is another far easier solution that was posted originally to my knowledge by Tim Riendeau:
http://www.wanlord.com/articles/2007/11/29/sending-email-using-actionmailer-and-gmail
I will repeat the steps as they worked for me here…
1. Install the tlsmail gem
sudo gem install tlsmail
2. Add the following to the bottom of your environment.rb
require ‘tlsmail’
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => ‘smtp.gmail.com’,
:tls => ‘true’,
:port => 587,
:domain => ‘mydomain.com’,
:authentication => :plain,
:user_name => ‘info@mydomain.com’,
:password => ‘mypassword’
}
3. To enable errors to show up if the mail does not get delivered, modify your development.rb and production.rb as shown below – it’s ok to make this true in devlopment, but i think most people say to set this to false in production:
development.rb
config.action_mailer.raise_delivery_errors = true
production.rb
config.action_mailer.raise_delivery_errors = false
And that’s it!
NOTE: “Timeout::Error (execution expired)”
I got this message quite a lot when i tried to send emails in development, and no one seems to know why this is the case. But when i switched to production things worked like a breeze. I am developing on a MacBook Pro with Leopard, if you have any idea why this happens please let me know.
NOTE: Internet Service Provider blocking ports
Another point to note is that some ISP’s block outgoing smtp mail on certain ports, eg port 25. Check with your ISP if they do any port filtering.
Thank God I found this. I was getting really annoyed trying to figure out how to make Anatol Pomozov’s solution work (which is what most of the Rails + Gmail tutorials seem to use). I’m running Rails 2 on Windows at the moment, and your solution works for me!
Yeah, this has got to be the easiest way by far.