Multiple gmail accounts in Rails

7 April 2009 by Bret

Figuring out how to use Gmail, aka Google Apps, to send email within a Rails application is pretty straight forward. A couple Google searches and you’re all set. Rails guides has a basic overview that is pretty straight forward.

First, let’s go over quickly why you would want to use multiple Gmail accounts.

  1. You don’t want every email to be sent from the same account. You might want some from contact some from no-reply or support.
  2. The free version of Google Apps limits you to 500 emails a day. If you think you might send more than that, you’ll need to use more than one account. Or pay for an upgrade.

In our case, we don’t know how busy DanceSignUp.com is going to be. So the 500 limit isn’t quite an issue, yet. It would be a nice problem to have. For us it’s more about sending certain types of email from certain accounts.

When a new user signs up, we want the email to go out from signup, when they forget their password no-reply is fine. If a support ticket response is submitted the email should be from support.

A problem remains however, how to use multiple Gmail accounts. I’ve been Googling for a while now and just figured I was the idiot and that perhaps the solution is so easy that no one needed to blog or write about it. Well regardless, hopefully this solution will help someone.

This code works in Ruby 1.8.7 and Rails 2.3.2. Given your basic SMTP setup in your respective environments file:

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

Make this the default account you want to send email out from. Now to send email from a different account for a different set of actions, open your mailer model file. We'll say it'scontact_mailer.rb.

Since the emails that are sent out from this mailer are specific to a contact form being submitted by a user, we want the email to come from contact. This account is not monitored for email, it’s used purely to send email.

Our contact_mailer.rb code looks like this.

class ContactMailer < ActionMailer::Base
 layout 'email'

 def admin_notify(contact)
 subject       'DanceSignUp Contact Form: - ' + contact.title
 recipients    'admin_email_address'
 from          'contact@domain.com'
 sent_on       Time.now
 body          :message => contact
 content_type  "text/html"
 end

 def user_notify(contact)
 subject       'Thank you for contacting DanceSignUp.com'
 recipients    contact.email
 from          'contact@domain.com'
 sent_on       Time.now
 body          :message => contact
 content_type  "multipart/alternative"
 end
end

At the top of mailer, before our first method and after our layout call we’ll add.

ContactMailer.smtp_settings = {
 :address => "smtp.gmail.com",
 :port => 587,
 :domain => "domain.com",
 :user_name => "contact@domain.com",
 :password => "password",
 :authentication => :plain,
 :enable_starttls_auto => true
}

That’s all we need, now all the methods in the ContactMailer will be sent out from contact. I did have to break out a couple of methods for forgotten passwords into a separate mailer so that those emails would be sent out from no-reply. But that was about all the tweaking needed.

Bookmark and Share

Tags: , , ,

This entry was posted on Tuesday, April 7th, 2009 at 8:34 am and is filed under Ruby on Rails. You can follow any responses to this entry through the RSS feed. Both comments and pings are currently closed.

Comments are closed.