From b2135b65766299e5550fb88e26cec073ccd7f1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chai=CC=88b=20Martinez?= Date: Mon, 16 Sep 2019 17:14:46 +0200 Subject: [PATCH] Sendinblue email balancing using interceptor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Chaìˆb Martinez --- .../dynamic_smtp_settings_interceptor.rb | 16 +++++++++++ config/env.example | 7 ++++- config/environments/development.rb | 28 +++++++++++++------ config/environments/production.rb | 10 +++++++ .../dynamic_smtp_settings_interceptor.rb | 1 + config/secrets.yml | 1 + 6 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 app/models/dynamic_smtp_settings_interceptor.rb create mode 100644 config/initializers/dynamic_smtp_settings_interceptor.rb diff --git a/app/models/dynamic_smtp_settings_interceptor.rb b/app/models/dynamic_smtp_settings_interceptor.rb new file mode 100644 index 000000000..bbd702420 --- /dev/null +++ b/app/models/dynamic_smtp_settings_interceptor.rb @@ -0,0 +1,16 @@ +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_CLIENT_KEY'], + address: 'smtp-relay.sendinblue.com', + domain: 'smtp-relay.sendinblue.com', + port: '587', + authentication: :cram_md5 + } + end + end + end +end diff --git a/config/env.example b/config/env.example index 4f704adab..143e586ed 100644 --- a/config/env.example +++ b/config/env.example @@ -46,8 +46,13 @@ SENTRY_DSN_JS="" MATOMO_ENABLED="disabled" MATOMO_ID="73" -SENDINBLUE_ENABLED="disabled" +SENDINBLUE_BALANCING="" +SENDINBLUE_BALANCING_VALUE="" +SENDINBLUE_ENABLED="" SENDINBLUE_CLIENT_KEY="" +SENDINBLUE_USER_NAME="" + + CRISP_ENABLED="disabled" CRISP_CLIENT_KEY="" diff --git a/config/environments/development.rb b/config/environments/development.rb index 385185b36..1018ab6cf 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -45,14 +45,26 @@ Rails.application.configure do config.assets.raise_runtime_errors = true # Action Mailer settings - config.action_mailer.delivery_method = :letter_opener_web - # Configure default root URL for generating URLs to routes - config.action_mailer.default_url_options = { - host: 'localhost', - port: 3000 - } - # Configure default root URL for email assets - config.action_mailer.asset_host = "http://" + ENV['APP_HOST'] + + 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[:client_key], + address: 'smtp-relay.sendinblue.com', + domain: 'smtp-relay.sendinblue.com', + port: '587', + authentication: :cram_md5 + } + else + config.action_mailer.delivery_method = :letter_opener_web + config.action_mailer.default_url_options = { + host: 'localhost', + port: 3000 + } + + config.action_mailer.asset_host = "http://" + ENV['APP_HOST'] + end Rails.application.routes.default_url_options = { host: 'localhost', diff --git a/config/environments/production.rb b/config/environments/production.rb index 8837fb88b..38de5db80 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -77,6 +77,16 @@ 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[:client_key], + address: 'smtp-relay.sendinblue.com', + domain: 'smtp-relay.sendinblue.com', + port: '587', + authentication: :cram_md5 + } else config.action_mailer.delivery_method = :mailjet end diff --git a/config/initializers/dynamic_smtp_settings_interceptor.rb b/config/initializers/dynamic_smtp_settings_interceptor.rb new file mode 100644 index 000000000..a3f8e2d13 --- /dev/null +++ b/config/initializers/dynamic_smtp_settings_interceptor.rb @@ -0,0 +1 @@ +ActionMailer::Base.register_interceptor "DynamicSmtpSettingsInterceptor" diff --git a/config/secrets.yml b/config/secrets.yml index 7ad675ffe..c0989401f 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -54,6 +54,7 @@ defaults: &defaults webhook_secret: <%= ENV['HELPSCOUT_WEBHOOK_SECRET'] %> sendinblue: enabled: <%= ENV['SENDINBLUE_ENABLED'] == 'enabled' %> + username: <%= ENV['SENDINBLUE_USER_NAME'] %> client_key: <%= ENV['SENDINBLUE_CLIENT_KEY'] %> api_v3_key: <%= ENV['SENDINBLUE_API_V3_KEY'] %> matomo: