December 11, 2008
Older: HTTParty Divorces ActiveSupport
Newer: When You Don't Know, Test
Using Gmail to Send Email With Rails
I know this has been covered before but since it was suggested, I thought I would mention it here. Previously, I showed how to receive email in your Rails apps using Gmail, so it only makes sense that I would also show how to send email with Gmail.
Caveat emptor
First things first. I would not recommend using Gmail for something important that sends a lot of email. If you are going to be sending a lot of email, take PJ’s advice and use AuthSMTP or some other similar service. If, however, you are setting up something small and don’t feel like setting up Postfix or dropping some benjamins, Gmail works just fine.
Add An Initializer (or Gem)
As you may or may not know, Gmail requires tls, but net/smtp and thus ActionMailer, do not support tls out of the box. Luckily, there is a chunk of code that is floating all over the internets that adds this functionality. I typically put this code in config/initializers/smtp_tls.rb
.
I so rarely use this that I haven’t taken the time to gem it up. Thankfully, Jason Perry did and posted so in a comment below. If you would rather have the gem dependency than the initializer, you can add the following to your environment.rb file.
config.gem "ambethia-smtp-tls", :lib => "smtp-tls", :source => "http://gems.github.com/"
Configure Production SMTP Settings
Then, once I have the initializer in place, I add the following to the bottom of config/environments/production.rb
.
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "monkey@domain.com",
:authentication => :plain,
:user_name => "monkey@domain.com",
:password => "banana"
}
The End
Sorry, that is the end. From this point on, just script/generate mailer
as you normally would. In development mode, your emails will show up in your log file and in production, they will be sent using Gmail’s smtp settings. That is really all you need to know. Hope this helps!
10 Comments
Dec 12, 2008
Instead of putting that “vendor” code in an initializer in all your apps, just gem install this: http://github.com/ambethia/smtp-tls/tree/master
And put this in environment.rb:
I wrapped up that same “bit-o-code” that’s been floating around to make it a little more reusable. I hate having to google around for it everytime I setup mailers.
Cheers!
Dec 12, 2008
Yes, alas, I have been too lazy to gem this up as I very rarely use it. Thanks for doing the work!
Dec 13, 2008
If you receive the following error: “ArgumentError: wrong number of arguments (3 for 2)” see my post for solving it.
Dec 13, 2008
Beware, I used this for a day and was losing at least 5% of emails into the ether. That was with fairly light usage.
Dec 14, 2008
Beware, Gmail imposes a send limit of 500 recipients per day, or 2000 if you’re on a paid account. Their solution? Open multiple accounts and spread the load among all of them. no-reply1@foo.com, no-reply2@foo.com, etc.
Dec 14, 2008
@David – I haven’t noticed that but thanks for mentioning it.
@rick – Good point. Exactly why I mention not using it if you are going to send a lot of email. I would only use Gmail if I was sending < 50 emails a day. Most of the apps I use it for maybe send 5-10 a day and only during certain parts of the year so it works out just fine.
Dec 15, 2008
Worth noting though that if you are running Rails 2.2 and Ruby 1.8.7, then it just works!
Dec 15, 2008
@tekin – Sweet. I did not know that.
Jan 05, 2009
Thanks for sharing!
Jan 09, 2009
Thanks a lot!!!
Sorry, comments are closed for this article to ease the burden of pruning spam.