Posts Tagged ‘rails’

Sending Email from Multiple Google Apps Accounts

20 October 2009 by Bret

PROBLEM: We need to be able to send emails from multiple accounts using Rails 2.3.4 and Google Apps. Our code was mostly correct, it worked in development because the ActionMailer classes were being loaded with each request. But in production all the emails were coming from one account, the account defined in the last mailer model file, for us UserMailer.

SOLUTION: The patch attached in the second post. The patch fixes actionmailer/lib/action_mailer/base.rb. No need for details on why other than to say smtp_settings and sendmail_settings are changed into superclass_delegating_accessor’s so that mail settings can be changed in each mail class.

This is needed especially when using Google Apps Standard because Google limits outgoing email to 500 per account per day.

After patching the base.rb file, we add our code to our production environment 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
}

We tried just assigning the user_name and password in the mailer class and got errors with our smtp_tls code were using, so we just duplicate all the other settings as well and it works. Our sample_mailer.rb code:

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

This way we can now send from whatever email account we need to for each mailer.

Multiple Ruby Versions for Development

10 September 2009 by Bret

We’ve been working through bug after bug in Ruby 1.9.1 and Rails 2.3.4 all in the effort of getting our new product launched. I don’t want to say we’ve given up and retreated, but rather, we’ve put Ruby 1.9.1 to the side for a while. There are just too many things that need patching and fixed just to get things to run normally.

We’re looking at Ruby Enterprise Edition by Phusion, and we’re pretty excited by the features and speed. We just want to go back, either to 1.8.7 or even 1.8.6 cause outside of 1.9.1 not only do normal things work, like sending an email from someone: John Locke < jlocke@theisland.com > instead of just a nondescript email address: jlocke@theisland.com But the site just seems way faster than when it was on 1.9.1.

To downgrade my development Ruby and make like easier when we want to return to 1.9.x, I turned to Ruby Version Manager. RVM is a great little gem written by Wayne Seguin that was just updated yesterday. RVM allows you to switch between Ruby versions, including Ruby Enterprise Edition and JRuby as well as using what RVM calls gem sets.

This allows me to install different versions of Rails against a particular version of Ruby. For example, by just typing: rvm ree -m rails234 I can set up a gem set called rails234 where I can install Rails 2.3.4 using gem install rails -v 2.3.4 and test it using Ruby Enterprise Edition. If I want to install Rails 2.3.3 I do the same thing: rvm ree -m rails233 then gem install rails -v 2.3.3

Switching between versions is as easy as rvm ree or rvm 1.9.1 You can find out more at the RVM site. But I have to say Ruby development has never been so easy. Thanks for a great gem Wayne.

Ruby on Rails 2.3.4 not playing well with Ruby 1.9.1

6 September 2009 by Bret

I was hoping upgrading from Rails 2.3.3 to 2.3.4 was going to be a breeze. Nope. Apparently, there are some issues with Rails 2.3.4 and Ruby 1.9.1 as detailed in this lighthouse ticket.

The problem is in the active_support-2.3.4/lib/active_support/message_verifier.rb file. When you try and login to your Rails app you’ll receive an error like:
undefined method `^' for "9":String

I won’t get into the details as to what’s breaking, that’s for those much smarter than I.

Basically if you’re using Ruby 1.9.1, you might be better off not upgrading to 2.3.4 as your application will break. I applied the patch in the ticket and all works now, but what a pain. Seems like something that should have been tested before 2.3.4 was released. Others have had the same issue as well.

To apply the patch I downloaded the patch from the lighthouse ticket above to my desktop and then used:
cd /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.4
sudo patch -p1 < /local/path/to/patch/0001-ruby-1.9-friendly-secure_compare.patch

The file that needed to be patched couldn't be found so i got:
File to patch:
Where I entered the path to the file:
lib/active_support/message_verifier.rb

The patch then applied and I was once again able to login to my application.