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.
This commit is contained in:
songtianlun 2025-01-08 13:27:28 +08:00
parent 32ec61fe00
commit 494e12bb9e
2 changed files with 8 additions and 7 deletions

View File

@ -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
}

View File

@ -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