Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Fix relay-without-auth MUA config (#4298)

ruslandoga530951780c8c
diff --git a/config/runtime.exs b/config/runtime.exs
index 02b9f11fc2..a19f802bfe 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
+
+ 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]
- config :plausible, Plausible.Mailer,
- auth: [username: username, password: password],
- relay: relay,
- port: port
+ 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 0ccfe18046..1802629e89 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