config: use alternate delivery methods to configure ActionMailer

Previously `SENDINBLUE_BALANCING` was used only when
`SENDINBLUE_ENABLED` was *disabled* (otherwise only SendInBlue was ever
used).

This commit:

- Ensure that `SENDINBLUE_BALANCING` is used only when SendInBlue is
  *enabled* (which is more intuitive).
- Make it easier to add other delivery methods.
This commit is contained in:
Pierre de La Morinerie 2022-01-18 16:56:30 +01:00
parent d3603cf3c5
commit 04cfc8ed9d
4 changed files with 43 additions and 24 deletions

4
app/lib/mailtrap/smtp.rb Normal file
View file

@ -0,0 +1,4 @@
module Mailtrap
class Smtp < ::Mail::SMTP
end
end

View file

@ -0,0 +1,4 @@
module Sendinblue
class Smtp < ::Mail::SMTP
end
end

View file

@ -1,4 +1,6 @@
require "active_support/core_ext/integer/time"
require Rails.root.join("app/lib/mailtrap/smtp")
require Rails.root.join("app/lib/sendinblue/smtp")
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
@ -76,8 +78,8 @@ Rails.application.configure do
# config.action_mailer.raise_delivery_errors = false
if ENV['MAILTRAP_ENABLED'] == 'enabled'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
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',
@ -85,17 +87,22 @@ Rails.application.configure do
port: '2525',
authentication: :cram_md5
}
elsif 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
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)
config.action_mailer.delivery_method = :mailjet
end

View file

@ -6,17 +6,21 @@
class DynamicSmtpSettingsInterceptor
def self.delivering_email(message)
if ENV['SENDINBLUE_BALANCING'] == 'enabled'
if rand(0..99) < ENV['SENDINBLUE_BALANCING_VALUE'].to_i
message.delivery_method.settings = {
user_name: ENV['SENDINBLUE_USER_NAME'],
password: ENV['SENDINBLUE_SMTP_KEY'],
address: 'smtp-relay.sendinblue.com',
domain: 'smtp-relay.sendinblue.com',
port: '587',
authentication: :cram_md5
}
end
if balance_to_sendinblue?
ApplicationMailer.wrap_delivery_behavior(message, :sendinblue)
end
# fallback to the default delivery method
end
private
def self.balance_to_sendinblue?
if ENV.fetch('SENDINBLUE_ENABLED') != 'enabled'
false
elsif ENV.fetch('SENDINBLUE_BALANCING') == 'enabled'
rand(0..99) < ENV.fetch('SENDINBLUE_BALANCING_VALUE').to_i
else
true
end
end
end