From 530951780c8cd0bd8c7d8c93cb08bc35d3f5056f Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Mon, 15 Jul 2024 17:01:57 +0700 Subject: [PATCH] Fix relay-without-auth MUA config (#4298) --- config/runtime.exs | 23 +++++++++++++++++------ test/plausible/config_test.exs | 20 ++++++++++++++++++-- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index 02b9f11fc..a19f802bf 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -504,13 +504,24 @@ case mailer_adapter do if relay = get_var_from_path_or_env(config_dir, "SMTP_HOST_ADDR") do port = get_int_from_path_or_env(config_dir, "SMTP_HOST_PORT", 25) - username = get_var_from_path_or_env(config_dir, "SMTP_USER_NAME") - password = get_var_from_path_or_env(config_dir, "SMTP_USER_PWD") + config :plausible, Plausible.Mailer, relay: relay, port: port + end - config :plausible, Plausible.Mailer, - auth: [username: username, password: password], - relay: relay, - port: port + username = get_var_from_path_or_env(config_dir, "SMTP_USER_NAME") + password = get_var_from_path_or_env(config_dir, "SMTP_USER_PWD") + + cond do + username && password -> + config :plausible, Plausible.Mailer, auth: [username: username, password: password] + + username || password -> + raise ArgumentError, """ + Both SMTP_USER_NAME and SMTP_USER_PWD must be set for SMTP authentication. + Please provide values for both environment variables. + """ + + _both_nil = true -> + nil end "Bamboo.LocalAdapter" -> diff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exs index 0ccfe1804..1802629e8 100644 --- a/test/plausible/config_test.exs +++ b/test/plausible/config_test.exs @@ -159,9 +159,25 @@ defmodule Plausible.ConfigTest do assert get_in(runtime_config(env), [:plausible, Plausible.Mailer]) == [ {:adapter, Bamboo.Mua}, - {:auth, [username: "neo", password: "one"]}, {:relay, "localhost"}, - {:port, 2525} + {:port, 2525}, + {:auth, [username: "neo", password: "one"]} + ] + end + + test "Bamboo.Mua (no auth relay config)" do + env = [ + {"MAILER_ADAPTER", "Bamboo.Mua"}, + {"SMTP_HOST_ADDR", "localhost"}, + {"SMTP_HOST_PORT", "2525"}, + {"SMTP_USER_NAME", nil}, + {"SMTP_USER_PWD", nil} + ] + + assert get_in(runtime_config(env), [:plausible, Plausible.Mailer]) == [ + adapter: Bamboo.Mua, + relay: "localhost", + port: 2525 ] end