Search code examples
ruby-on-railsgmailactionmailer

action_mailer config in its own file


I don't want to put the account and password for our gmail account in the production app config file.

Rather I would like to set things up so that this information is in a separate yml file that will not be stored on our git repository. We do this with other data that needs to be secured, but I don't see a straight forward way to do it with action_mailer.

In otherwords I want action_mailer to read its config info from something like action_mailer_config.yml NOT from the environments/production.rb config file.


Solution

  • Just create an initializer file that assigns an environmental variable. So in your production environment you would have:

       config.action_mailer.smtp_settings = {
        address: "smtp.gmail.com",
        port: 587,
        domain: "chicheng.com.tw",
        authentication: "plain",
        enable_starttls_auto: true,
        user_name: ENV["GMAIL_USERNAME"],
        password: ENV["GMAIL_PASSWORD"],
        :openssl_verify_mode  => 'none'
        }
    

    Then you add an initializer file that assigns the username and password like this:

    ENV["GMAIL_USERNAME"] = "[email protected]"
    ENV["GMAIL_PASSWORD"] = "your_password"
    

    One important thing to remember is that you add this initializer file to your git ignore. You would then need to manually make sure this file is included when you push it up.

    To be honest instead of using a separate file, I prefer just setting the ENV on the server directly. This way I do not have to worry about the file accidentally getting pushed.