LoginSignup
3
0

More than 3 years have passed since last update.

credentials.yml.encの使用法

Last updated at Posted at 2020-04-18

環境変数設定にcredentials.yml.encを使用しました。
お問い合わせフォーム作成のためにGメールアドレスと2段階認証のパスワードが必要だったので、環境変数に設定しました。

参考
https://qiita.com/akizora/items/936f57dd2d3cb11fc840

credentials.yml.encの編集

ターミナル

$ EDITOR="vim" bin/rails credentials:edit

iで入力モードになります。

認証情報を記載

ターミナル

# aws:
#   access_key_id: 123
#   secret_access_key: 345

 g_mail:
    g_mail: Gメールアドレス
    g_pass: 2段階認証パスワード

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 8be8e637d755f79c799048bed8be0c...

:の後には必ず半角スペースを入れてください。
escキー :wqで終了

入力の確認

ターミナル

$ rails c

irb(main):001:0> Rails.application.credentials.g_mail[:g_mail]
=> Gメールアドレス

irb(main):002:0> Rails.application.credentials.g_mail[:g_pass]
=> 2段階認証パスワード

確認ができればOKです。

コードの入力

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address => "smtp.gmail.com",
      :port => 587,
      :domain => "gmail.com",
      :user_name => Rails.application.credentials.g_mail[:g_mail], # 送信先アドレス
      :password => Rails.application.credentials.g_mail[:g_pass], #2段階認証パスワード
      :authentication => :plain,
      :enable_starttls_auto => true
  }

こんな感じでセットができます。

最後に

環境変数の設定は使用頻度も多いので、習得できてよかったです。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0