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.
