Configuring SMTP Setting dynamically

SMTP is mail delivery method in rails  which is used to send email through rails. Setting of SMTP is configured in different environment like development, production and test. For this we configure following setting in different file like config/environments/development.rb, config/environments/production.rb, config/environments/test.rb with respect to different environment.

 config.action_mailer.smtp_settings = {
address: “smtp.gmail.com”,
port: 587,
domain: “mail.google.com”,
user_name: “example@gmail.com”,
password: “********”,
authentication: “plain”,
enable_starttls_auto: true
}

But sometime we need to set up smtp attributes like address, domain, port etc. dynamically on run time then these above method gives some problem. Because file on the cofig folder loaded when we start the server. For this we configure smtp settings in application controller. The configuration is as below.

before_filter :set_mailer_settings

private
def set_mailer_settings
options = Mails.first()
ActionMailer::Base.smtp_settings = {
:address => options["smtp_address"],
:port => options["smtp_port"],
:domain => options["smtp_domain"],
:authentication => options["authentication_type"],
:user_name => options["smtp_username"],
:password => options["smtp_password"],
}
end

Where Mails is a model from which attributes of smtp are ftech. By this way we can configure smtp setting dynamically in rails on run time.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s