From 31996c7d09b5fe11474dcabc69e1ab1137f77b84 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 25 Jan 2022 15:14:58 +0100 Subject: [PATCH] chore(smtp): add mailcatcher configuration --- config/env.example | 8 +++++++- config/environments/production.rb | 3 ++- config/initializers/mailcatcher.rb | 13 +++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 config/initializers/mailcatcher.rb diff --git a/config/env.example b/config/env.example index 4c6bb5cb0..9a3fc6d54 100644 --- a/config/env.example +++ b/config/env.example @@ -90,11 +90,17 @@ SENDINBLUE_BALANCING="disabled" SENDINBLUE_BALANCING_VALUE="50" # Alternate SMTP Provider: Mailtrap (mail catcher for staging environments) -# When enabled, all emails will be sent using this provided +# When enabled, all emails will be sent using this provider MAILTRAP_ENABLED="disabled" MAILTRAP_USERNAME="" MAILTRAP_PASSWORD="" +# Alternative SMTP Provider: Mailcatcher (Catches mail and serves it through a dream.) +# When enabled, all emails will be sent using this provider +MAILCATCHER_ENABLED="disabled" +MAILCATCHER_HOST="" +MAILCATCHER_PORT="" + # External service: live chat for admins (specific to démarches-simplifiées.fr) CRISP_ENABLED="disabled" CRISP_CLIENT_KEY="" diff --git a/config/environments/production.rb b/config/environments/production.rb index 912426727..9c541639d 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -89,7 +89,8 @@ Rails.application.configure do elsif ENV['SENDINBLUE_ENABLED'] == 'enabled' config.action_mailer.delivery_method = :sendinblue - + elsif ENV['MAILCATCHER_ENABLED'] == 'enabled' + config.action_mailer.delivery_method = :mailcatcher else config.action_mailer.delivery_method = :mailjet end diff --git a/config/initializers/mailcatcher.rb b/config/initializers/mailcatcher.rb new file mode 100644 index 000000000..8b931f704 --- /dev/null +++ b/config/initializers/mailcatcher.rb @@ -0,0 +1,13 @@ +if ENV.fetch('MAILCATCHER_ENABLED') == 'enabled' + ActiveSupport.on_load(:action_mailer) do + module Mailcatcher + class SMTP < ::Mail::SMTP; end + end + + ActionMailer::Base.add_delivery_method :mailcatcher, Mailcatcher::SMTP + ActionMailer::Base.mailcatcher_settings = { + address: ENV.fetch("MAILCATCHER_HOST"), + port: ENV.fetch("MAILCATCHER_PORT") + } + end +end