Fabian
Fabian

Reputation: 133

Rails: Runtime configuration of ActionMailer?

I would like to send a small amount of email from my app through Gmail. Now, the SMTP settings will be determined at runtime (ie: from the db), can this be done?

--- edit ---

I can set the ActionMailer subclass (named Notifier) smtp settings in one of the class' methods. This way I can set the username and password for sending the email dynamically. The only thing is that you have to set ALL the smtp_settings. Is it possible to set just the username & password settings in the class method?

This is the code I'm using right now, it is sending:

class Notifier < ActionMailer::Base
  def call(user)
    Notifier.smtp_settings = { 
      :enable_starttls_auto => true, 
      :address => "smtp.gmail.com",
      :port => "587",
      :domain => "mydomain.com",
      :authentication => :plain,
      :user_name => "[email protected]",
      :password => "password"
    }

    recipients user.email
    subject    "Test test"
    body       "Test"
  end
end

I would like to just set the username and pw here.

Upvotes: 3

Views: 3601

Answers (4)

Alejo Dev
Alejo Dev

Reputation: 2586

You can do that in the controller:

class ApplicationController < ActionController::Base

   ...

private

  def set_mailer_settings

    ActionMailer::Base.smtp_settings.merge!({
      username: 'username',
      password: 'yoursupersecretpassword'
    })

  end

end

Upvotes: 0

zigomir
zigomir

Reputation: 985

(Rails 3)

Since I call mailer like this:

CustomerMailer.customer_auto_inform(@form).deliver

In CustomerMailer class I have private method:

def init_email_account(shop_mail)
  ActionMailer::Base.raise_delivery_errors = true
  ActionMailer::Base.smtp_settings = {
    :address              => shop_mail.address,
    :port                 => shop_mail.port,
    :domain               => shop_mail.domain,
    :user_name            => shop_mail.user_name,
    :password             => shop_mail.password,
    :authentication       => shop_mail.authentication.name,
    :enable_starttls_auto => shop_mail.enable_starttls_auto
  }
end

Before calling mail() which sends email you need to call private method init_email_account to populate smtp_settings from database. shop_mail is model which stores the data about mail account settings.

HTH

Upvotes: 2

Jeff Paquette
Jeff Paquette

Reputation: 7127

Assuming you've configured the smtp_settings in your environment or initializers, you can just set the username like so:

Notifier.smpt_settings.merge!({:user_name => "x", :password=> "y"})

Upvotes: 0

Douglas F Shearer
Douglas F Shearer

Reputation: 26488

Since the configuration files are all Ruby, then the settings can easily be fetched from a configuration file or the like at runtime.

Here's a post I wrote a while back on getting ActionMailer working with GMail SMTP.

NOTE: If you're using rails 2.3 and Ruby 1.87, you don't need the plugin and can simply use the settings in this comment

Upvotes: 0

Related Questions