Mohith Thimmaiah
Mohith Thimmaiah

Reputation: 857

Rails - using multiple email providers in single app

Anyway I can use multiple email providers within the same Rails 3 app ?

Context 1. Im using postmark for sending out mails currently (using delayed job) 2. Our app also needs to send out some mass emails - for which we will be using a separate provider.

Now I dont want to separate out and create a new app for the mass emailing part. How can I use/choose different email providers at the point of sending email ?

Thanks in advance

Upvotes: 3

Views: 1327

Answers (3)

shime
shime

Reputation: 9008

I'm using mailserver fallback in my application, so when one mail server is down it switches mailserver. Your problem is similar, except you don't need to alias the old Mail::Message.deliver and use Mail::Message.mass_deliver for instance.

This is how you do it:

Mail::Message.class_eval do
  def mass_deliver
     self.delivery_method.settings = {
                                      :address => "smtp.massdeliverserver.com",
                                      :port => 587,
                                      :domain => 'yourdomain.com',
                                      :user_name => '[email protected]',
                                      :password => 'yourpassword',
                                      :authentication => 'plain',
                                      :enable_starttls_auto => true
                                     }
     deliver
  end
end

Then you could use YourMailer.your_method.deliver to use defalt settings you provided in environment.rb for config.action_mailer.smtp_settings and YourMailer.your_method.mass_deliver to use the other server settings.

Put the code inside some file in config/initializers and mass_deliver method will be available for any Mail::Message instance in your application.

Upvotes: 1

Frederick Cheung
Frederick Cheung

Reputation: 84124

You can override ActionMailer settings on a per mailer basis, for example

class BulkMailer < ActionMailer::Base
  self.smtp_settings = {...}
end

will cause BulkMailer and its subclasses to use those settings.

The one thing to be wary of is not to change smtp_settings in place, i.e. do not do something like self.smtp_settings[:user_name] = 'blah' as this would be acting on the shared settings rather than creating new settings private to BulkMailer

Upvotes: 7

Kaushik Thirthappa
Kaushik Thirthappa

Reputation: 1051

You have a mass email list to which you need to send out from say [email protected] and some other emails for other purpose from [email protected]

You need to do these steps if i am getting the question correct ::

  1. Remove the default from default :from if you have written it.

  2. Create an action mailer for mass-email and put up the :from => "[email protected]"

    1. Go to your environment.rb file and fill up the details like this

    config.action_mailer.smtp_settings = {

    :address => "smtp.gmail.com",

    :port => 587,

    :domain => 'yourdomain.com',

    :user_name => '[email protected]',

    :password => 'yourpassword',

    :authentication => 'plain',

    :enable_starttls_auto => true }

You can create it for as many files as you wish.

Hope it helps.

Upvotes: 0

Related Questions