Skip to content

Commit 95d3839

Browse files
authored
Improve default action mailer configuration (rails#52513)
* Suggest using Rails credentials for STMP username and password * Set a default host for mailer template links in production too * Homogenize comment of mailer link host setting * Just use the default from address set by ApplicationMailer
1 parent e27e39b commit 95d3839

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Rails.application.configure do
4545
# caching is enabled.
4646
config.action_mailer.perform_caching = false
4747

48+
# Set localhost to be used by links generated in mailer templates.
4849
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
4950
<%- end -%>
5051

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ Rails.application.configure do
9191
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
9292
# config.action_mailer.raise_delivery_errors = false
9393

94+
# Set host to be used by links generated in mailer templates.
95+
config.action_mailer.default_url_options = { host: "example.com" }
96+
97+
# Specify outgoing SMTP server. Remember to add smtp/* credentials via rails credentials:edit.
98+
# config.action_mailer.smtp_settings = {
99+
# user_name: Rails.application.credentials.dig(:smtp, :user_name),
100+
# password: Rails.application.credentials.dig(:smtp, :password),
101+
# address: "smtp.example.com",
102+
# port: 587,
103+
# authentication: :plain
104+
# }
105+
94106
<%- end -%>
95107
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
96108
# the I18n.default_locale when a translation cannot be found).

railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ Rails.application.configure do
4444
# ActionMailer::Base.deliveries array.
4545
config.action_mailer.delivery_method = :test
4646

47-
# Unlike controllers, the mailer instance doesn't have any context about the
48-
# incoming request so you'll need to provide the :host parameter yourself.
49-
config.action_mailer.default_url_options = { host: "www.example.com" }
47+
# Set host to be used by links generated in mailer templates.
48+
config.action_mailer.default_url_options = { host: "example.com" }
5049

5150
<%- end -%>
5251
# Print deprecation notices to the stderr.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PasswordsMailer < ApplicationMailer
22
def reset(user)
33
@user = user
4-
mail subject: "Reset your password", to: user.email_address, from: "[email protected]"
4+
mail subject: "Reset your password", to: user.email_address
55
end
66
end

railties/lib/rails/generators/rails/credentials/templates/credentials.yml.tt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# smtp:
2+
# user_name: my-smtp-user
3+
# password: my-smtp-password
4+
#
15
# aws:
26
# access_key_id: 123
37
# secret_access_key: 345

0 commit comments

Comments
 (0)