Question

I'm configuring my app to send emails through gmail smtp, so I'm writing the action_mailer setup in the production.rb file.

Though I don't want to have my gmail credentials written in the production.rb file, which is git versioned.

this is what I have made so far:

in production rb:

  require "SmtpData"

  config.action_mailer.default_url_options = {:host => 'my server's ip'}
  config.action_mailer.delivery_method = :smtp
   config.action_mailer.smtp_settings = {
    enable_starttls_auto: "true",
    address: SmtpData::ADDRESS,
    port: SmtpData::PORT,
    domain: "pjforex.com",
    authentication: :plain,
    :user_name            => SmtpData::USER,
    :password             => SmtpData::PASS
  }

then I created config/initializers/smtp_data.rb containing:

class SmtpData
  USER = 'user@gmail.com'
  PASS = 'password'
  ADDRESS = "smtp.gmail.com"
  PORT = "587"
end

But when my capistrano tries to precompile the assets, I get :

rake aborted!
uninitialized constant SmtpData

any clue on how to solve this. or better approach to this issue?

thanks,

Était-ce utile?

La solution

You need to store your configuration in a file excluded from Git but shared between deployments. Assuming you are using Capistrano 3, it would be something like this:

1) On your server create a file shared/config/smtp.yml (using YAML is not something crucial, but it's just cleaner for configs) in Capistrano root folder with the following content:

user: user@gmail.com
pass: your_password
address: smtp.gmail.com
port: 587

2) Add this file to your linked_files in config/deploy.rb (ideally, config/database.yml should be stored this way too):

set :linked_files, %w{config/database.yml config/smtp.yml}

3) Read SMTP config in production.rb from config/smtp.yml.

4) You can also have config/smtp.yml locally for your development environment, but don't forget to add it to your .gitignore then.

Autres conseils

I believe that production.rb gets loaded before smtp_data.rb, which is why production.rb isn't able to access the class you have defined.

One solution we use, to keep sensitive credentials out of our git versions, is to create (as in your example) production.rb.template and add this to your git repository. This file is pretty much a copy of what you currently have for production.rb, except that you would have placeholders for where credentials should go. For example,

:username => USERNAME GOES HERE

Then, we remove production.rb from the repository (and, optionally, set it to be ignored by git). When you check out your local copy of the repository, you copy production.rb.template as production.rb, and you fill in the credential information. This will only exist in your local deployment and would not be pushed to git.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top