From b1a0268e6b52b9332b32ffd7dd648613685df6d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 22 Mar 2023 14:53:44 +0700 Subject: [PATCH] Make email settings configurable --- .env.example | 10 +++++- config/environments/development.rb | 6 +++- config/environments/production.rb | 53 ++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/.env.example b/.env.example index 5550c06..b667033 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,12 @@ +SMTP_SERVER=smtp.example.com +SMTP_PORT=587 +SMTP_LOGIN=accounts +SMTP_PASSWORD=123abc +SMTP_FROM_ADDRESS=accounts@example.com +SMTP_DOMAIN=example.com +SMTP_AUTH_METHOD=plain +SMTP_ENABLE_STARTTLS=auto + LDAP_HOST=localhost LDAP_PORT=389 LDAP_ADMIN_PASSWORD=passthebutter @@ -24,4 +33,3 @@ LNDHUB_PG_PORT=5432 LNDHUB_PG_DATABASE=lndhub LNDHUB_PG_USERNAME=lndhub LNDHUB_PG_PASSWORD='' - diff --git a/config/environments/development.rb b/config/environments/development.rb index 80a7540..b7d1659 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -57,10 +57,14 @@ Rails.application.configure do # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + config.action_mailer.default_options = { + from: "accounts@localhost" + } + # Don't actually send emails, cache them for viewing via letter opener config.action_mailer.delivery_method = :letter_opener # Don't care if the mailer can't send - config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true # Base URL to be used by email template link helpers config.action_mailer.default_url_options = { host: "localhost:3000", protocol: "http" } diff --git a/config/environments/production.rb b/config/environments/production.rb index 27b439c..1f22161 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -57,28 +57,53 @@ Rails.application.configure do # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "akkounts_production" - config.action_mailer.perform_caching = false + # E-mail settings, adapted from https://github.com/mastodon/mastodon - config.action_mailer.delivery_method = :smtp + outgoing_email_address = ENV.fetch('SMTP_FROM_ADDRESS', 'accounts@localhost') + outgoing_email_domain = Mail::Address.new(outgoing_email_address).domain + + config.action_mailer.default_options = { + from: outgoing_email_address, + message_id: -> { "<#{Mail.random_tag}@#{outgoing_email_domain}>" }, + } + + config.action_mailer.default_options[:reply_to] = ENV['SMTP_REPLY_TO'] if ENV['SMTP_REPLY_TO'].present? + config.action_mailer.default_options[:return_path] = ENV['SMTP_RETURN_PATH'] if ENV['SMTP_RETURN_PATH'].present? + + enable_starttls = nil + enable_starttls_auto = nil + + case ENV['SMTP_ENABLE_STARTTLS'] + when 'always' + enable_starttls = true + when 'never' + enable_starttls = false + when 'auto' + enable_starttls_auto = true + else + enable_starttls_auto = ENV['SMTP_ENABLE_STARTTLS_AUTO'] != 'false' + end config.action_mailer.smtp_settings = { - address: "mail.gandi.net", - port: "587", - authentication: "plain", - enable_starttls_auto: true, - user_name: Rails.application.credentials.smtp[:username], - password: Rails.application.credentials.smtp[:password] + port: ENV['SMTP_PORT'], + address: ENV['SMTP_SERVER'], + user_name: ENV['SMTP_LOGIN'].presence, + password: ENV['SMTP_PASSWORD'].presence, + domain: ENV['SMTP_DOMAIN'] || ENV['LOCAL_DOMAIN'], + authentication: ENV['SMTP_AUTH_METHOD'] == 'none' ? nil : ENV['SMTP_AUTH_METHOD'] || :plain, + ca_file: ENV['SMTP_CA_FILE'].presence || '/etc/ssl/certs/ca-certificates.crt', + openssl_verify_mode: ENV['SMTP_OPENSSL_VERIFY_MODE'], + enable_starttls: enable_starttls, + enable_starttls_auto: enable_starttls_auto, + tls: ENV['SMTP_TLS'].presence && ENV['SMTP_TLS'] == 'true', + ssl: ENV['SMTP_SSL'].presence && ENV['SMTP_SSL'] == 'true', } - config.action_mailer.default_url_options = { - host: "accounts.kosmos.org", - protocol: "https", - from: "accounts@kosmos.org" - } + config.action_mailer.delivery_method = ENV.fetch('SMTP_DELIVERY_METHOD', 'smtp').to_sym # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. - # config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found).