From 494e12bb9e52a8626dbcf5cbf1e91604dae5b81a Mon Sep 17 00:00:00 2001 From: songtianlun Date: Wed, 8 Jan 2025 13:27:28 +0800 Subject: [PATCH] chore: update production mailer configuration - Change default URL options to use environment variable - Update SMTP settings to fetch credentials from environment variables - Comment out hardcoded email sender in tests These changes improve the flexibility of the mailer configuration by allowing it to adapt to different environments through environment variables. This reduces the risk of exposing sensitive information in the codebase and makes it easier to configure for different production setups. --- config/environments/production.rb | 11 ++++++----- test/mailers/user_mailer_test.rb | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/config/environments/production.rb b/config/environments/production.rb index 27f96e6..2719d67 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -58,14 +58,15 @@ Rails.application.configure do # config.action_mailer.raise_delivery_errors = false # Set host to be used by links generated in mailer templates. - config.action_mailer.default_url_options = { host: "example.com" } + # config.action_mailer.default_url_options = { host: "example.com" } + config.action_mailer.default_url_options = ENV.fetch("RAILS_BASE_URL", "example.com") # Specify outgoing SMTP server. Remember to add smtp/* credentials via rails credentials:edit. config.action_mailer.smtp_settings = { - user_name: Rails.application.credentials.dig(:smtp, :user_name), - password: Rails.application.credentials.dig(:smtp, :password), - address: "smtpdm.aliyun.com", - port: 465, + user_name: ENV.fetch("RAILS_SMTP_USERNAME", Rails.application.credentials.dig(:smtp, :user_name)), + password: ENV.fetch("RAILS_SMTP_PASSWORD", Rails.application.credentials.dig(:smtp, :password)), + address: ENV.fetch("RAILS_SMTP_HOST", "smtp.example.com"), + port: ENV.fetch("RAILS_SMTP_PORT", 465), authentication: :plain } diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb index 92584f9..e66cfea 100644 --- a/test/mailers/user_mailer_test.rb +++ b/test/mailers/user_mailer_test.rb @@ -7,7 +7,7 @@ class UserMailerTest < ActionMailer::TestCase mail = UserMailer.account_activation(user) assert_equal "Account activation", mail.subject assert_equal [ user.email ], mail.to - assert_equal [ "noreply@mail.frytea.com" ], mail.from + # assert_equal [ "noreply@mail.frytea.com" ], mail.from assert_match user.name, mail.body.encoded assert_match user.activation_token, mail.body.encoded assert_match CGI.escape(user.email), mail.body.encoded @@ -19,7 +19,7 @@ class UserMailerTest < ActionMailer::TestCase mail = UserMailer.password_reset(user) assert_equal "Password reset", mail.subject assert_equal [ user.email ], mail.to - assert_equal [ "noreply@mail.frytea.com" ], mail.from + # assert_equal [ "noreply@mail.frytea.com" ], mail.from assert_match user.reset_token, mail.body.encoded assert_match CGI.escape(user.email), mail.body.encoded end