Created
September 22, 2011 21:57
-
-
Save guilherme/1236160 to your computer and use it in GitHub Desktop.
ActionMailer with more than one SMTP Account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is my action mailer setup and can be improved | |
# based on http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html | |
# config/initializer/action_mailer.yml | |
development: | |
default: | |
address: "smtp.provider.com" | |
port: 25 | |
domain: 'mywebsite.com' | |
user_name: 'mailer@mywebsite.com' | |
password: '12345' | |
authentication: "plain" | |
orders_mailer: | |
address: "smtp.provider.com" | |
port: 25 | |
domain: 'mywebsite.com' | |
user_name: 'orders@mywebsite.com' | |
password: '12345' | |
authentication: "plain" | |
# config/initializers/mailer_initializer.rb | |
class ActionMailer::Base | |
def self.load_settings(profile = :default) | |
settings = YAML.load_file("#{Rails.root}/config/action_mailer.yml")[Rails.env][profile.to_s] || {} | |
settings = settings.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} | |
if settings | |
default :delivery_method => :smtp | |
self.smtp_settings = settings | |
end | |
end | |
end | |
ActionMailer::Base.load_settings(:default) | |
# app/models/generic_mailer.rb | |
class GenericMailer < ActionMailer::Base | |
default :from => "mailer@mywebsite.com" | |
end | |
# app/models/order_mailer.rb | |
class OrderMailer < ActionMailer::Base | |
default :from => "orders@mywebsite.com" | |
load_settings :orders_mailer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment