Browse Source

Change the default SMTP settings

- Delete SMTP_USER_NAME and SMTP_PASSWORD

- Allow either SMTP_AUTHENTICATION=none or SMTP_USER_NAME=none to
  disable authentication

Closes #3415.
Akinori MUSHA 9 months ago
parent
commit
4d6f910ba6
2 changed files with 30 additions and 20 deletions
  1. 8 5
      .env.example
  2. 22 15
      config/initializers/action_mailer.rb

+ 8 - 5
.env.example

@@ -102,22 +102,25 @@ IMPORT_DEFAULT_SCENARIO_FOR_ALL_USERS=true
 # SMTP_AUTHENTICATION to login and the SMTP_PORT to 465.  
 # 
 # If you use a local SMTP server without authentication such as Postfix,
-# SMTP_USER_NAME must be set to none or else you will receive
-# errors that AUTH not enabled.
+# set SMTP_AUTHENTICATON to `none`.
 
 # Uncomment if you want to use `/usr/sbin/sendmail` to send email instead of SMTP.
 # This option is ignored unless RAILS_ENV=production, and setting it to `sendmail` causes the settings in the rest of this section (except EMAIL_FROM_ADDRESS) to be ignored.
 # SMTP_DELIVERY_METHOD=sendmail
 
 SMTP_DOMAIN=your-domain-here.com
-SMTP_USER_NAME=you@gmail.com
-SMTP_PASSWORD=somepassword
 SMTP_SERVER=smtp.gmail.com
 SMTP_PORT=587
-SMTP_AUTHENTICATION=plain
 SMTP_ENABLE_STARTTLS_AUTO=true
 SMTP_SSL=false
 
+SMTP_AUTHENTICATION=plain
+# SMTP_USER_NAME=you@gmail.com
+# SMTP_PASSWORD=somepassword
+
+# or without authentication:
+# SMTP_AUTHENTICATION=none
+
 # Set to true to send real emails via SMTP when running in the development Rails environment.
 # Set to false to have emails intercepted in development and displayed at http://localhost:3000/letter_opener
 SEND_EMAIL_IN_DEVELOPMENT=false

+ 22 - 15
config/initializers/action_mailer.rb

@@ -1,15 +1,22 @@
-ActionMailer::Base.smtp_settings = {
-  address: ENV['SMTP_SERVER'] || "smtp.gmail.com",
-  port: ENV['SMTP_PORT'] || 587,
-  domain: ENV['SMTP_DOMAIN'],
-  authentication: ENV['SMTP_AUTHENTICATION'] == 'none' ? nil : ENV['SMTP_AUTHENTICATION'] || "plain",
-  enable_starttls_auto: ENV['SMTP_ENABLE_STARTTLS_AUTO'] == 'true',
-  ssl: ENV['SMTP_SSL'] == 'true',
-  user_name: ENV['SMTP_USER_NAME'] == 'none' ? nil : ENV['SMTP_USER_NAME'].presence,
-  password: ENV['SMTP_USER_NAME'] == 'none' ? nil : ENV['SMTP_PASSWORD'].presence,
-  openssl_verify_mode: ENV['SMTP_OPENSSL_VERIFY_MODE'].presence,
-  ca_path: ENV['SMTP_OPENSSL_CA_PATH'].presence,
-  ca_file: ENV['SMTP_OPENSSL_CA_FILE'].presence,
-  read_timeout: ENV['SMTP_READ_TIMEOUT']&.to_i,
-  open_timeout: ENV['SMTP_OPEN_TIMEOUT']&.to_i,
- }
+ActionMailer::Base.smtp_settings = {}.tap { |config|
+  config[:address] = ENV['SMTP_SERVER'] || 'smtp.gmail.com'
+  config[:port] = ENV['SMTP_PORT']&.to_i || 587
+  config[:domain] = ENV['SMTP_DOMAIN']
+
+  authentication = ENV['SMTP_AUTHENTICATION'].presence || 'plain'
+  user_name = ENV['SMTP_USER_NAME'].presence || 'none'
+
+  if authentication != 'none' && user_name != 'none'
+    config[:authentication] = authentication
+    config[:user_name] = user_name
+    config[:password] = ENV['SMTP_PASSWORD'].presence
+  end
+
+  config[:enable_starttls_auto] = ENV['SMTP_ENABLE_STARTTLS_AUTO'] == 'true'
+  config[:ssl] = ENV['SMTP_SSL'] == 'true'
+  config[:openssl_verify_mode] = ENV['SMTP_OPENSSL_VERIFY_MODE'].presence
+  config[:ca_path] = ENV['SMTP_OPENSSL_CA_PATH'].presence
+  config[:ca_file] = ENV['SMTP_OPENSSL_CA_FILE'].presence
+  config[:read_timeout] = ENV['SMTP_READ_TIMEOUT']&.to_i
+  config[:open_timeout] = ENV['SMTP_OPEN_TIMEOUT']&.to_i
+}