config: simplify mailer configuration again

Move everything to initializers, and replace the email settings
interceptor by a BalancerDeliveryMethod.

It has the advantage that it can be configured entirely from the
`config/environment.rb` file, without an extra file to look at.
This commit is contained in:
Pierre de La Morinerie 2022-01-26 10:33:12 +00:00
parent 27b42fe8ae
commit 847abca122
11 changed files with 205 additions and 109 deletions

View file

@ -75,40 +75,13 @@ Rails.application.configure do
config.assets.raise_runtime_errors = true
# Action Mailer settings
config.action_mailer.delivery_method = :letter_opener
if ENV['SENDINBLUE_ENABLED'] == 'enabled'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
user_name: Rails.application.secrets.sendinblue[:username],
password: Rails.application.secrets.sendinblue[:smtp_key],
address: 'smtp-relay.sendinblue.com',
domain: 'smtp-relay.sendinblue.com',
port: '587',
authentication: :cram_md5
}
else
# https://usehelo.com
if ENV['HELO_ENABLED'] == 'enabled'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
user_name: 'demarches-simplifiees',
password: '',
address: '127.0.0.1',
domain: '127.0.0.1',
port: ENV.fetch('HELO_PORT', '2525'),
authentication: :plain
}
else
config.action_mailer.delivery_method = :letter_opener_web
end
config.action_mailer.default_url_options = {
host: 'localhost',
port: 3000
}
config.action_mailer.asset_host = "http://" + ENV['APP_HOST']
end
config.action_mailer.default_url_options = {
host: 'localhost',
port: 3000
}
config.action_mailer.asset_host = "http://" + ENV['APP_HOST']
Rails.application.routes.default_url_options = {
host: 'localhost',

View file

@ -1,6 +1,5 @@
require "active_support/core_ext/integer/time"
require Rails.root.join("app/lib/mailtrap/smtp")
require Rails.root.join("app/lib/sendinblue/smtp")
require Rails.root.join("app/lib/balancer_delivery_method")
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
@ -78,31 +77,20 @@ Rails.application.configure do
# config.action_mailer.raise_delivery_errors = false
if ENV['MAILTRAP_ENABLED'] == 'enabled'
ActionMailer::Base.add_delivery_method :mailtrap, Mailtrap::Smtp
config.action_mailer.mailtrap_settings = {
user_name: Rails.application.secrets.mailtrap[:username],
password: Rails.application.secrets.mailtrap[:password],
address: 'smtp.mailtrap.io',
domain: 'smtp.mailtrap.io',
port: '2525',
authentication: :cram_md5
}
config.action_mailer.delivery_method = :mailtrap
elsif
if ENV['SENDINBLUE_ENABLED'] == 'enabled'
ActionMailer::Base.add_delivery_method :sendinblue, Sendinblue::Smtp
config.action_mailer.sendinblue_settings = {
user_name: Rails.application.secrets.sendinblue[:username],
password: Rails.application.secrets.sendinblue[:smtp_key],
address: 'smtp-relay.sendinblue.com',
domain: 'smtp-relay.sendinblue.com',
port: '587',
authentication: :cram_md5
}
end
# Default delivery method
# (Actual delivery method will be selected at runtime by DynamicSmtpSettingsInterceptor)
elsif ENV['SENDINBLUE_ENABLED'] == 'enabled' && ENV['SENDINBLUE_BALANCING'] == 'enabled'
ActionMailer::Base.add_delivery_method :balancer, BalancerDeliveryMethod
config.action_mailer.balancer_settings = {
sendinblue: ENV.fetch('SENDINBLUE_BALANCING_VALUE').to_i,
mailjet: 100 - ENV.fetch('SENDINBLUE_BALANCING_VALUE').to_i
}
config.action_mailer.delivery_method = :balancer
elsif ENV['SENDINBLUE_ENABLED'] == 'enabled'
config.action_mailer.delivery_method = :sendinblue
else
config.action_mailer.delivery_method = :mailjet
end