refactor: unify smtp config logic
This commit is contained in:
parent
e95c1d6254
commit
e8f27690a1
6 changed files with 162 additions and 54 deletions
|
|
@ -226,11 +226,7 @@ if config_env() == :prod do
|
|||
# SMTP configuration from environment variables (overrides base adapter in prod).
|
||||
# When SMTP_HOST is set, configure Swoosh to use the SMTP adapter at boot time.
|
||||
# If SMTP is configured only via Settings (Admin UI), the mailer builds the config
|
||||
# per-send at runtime using Mv.Config.smtp_*() helpers.
|
||||
#
|
||||
# TLS/SSL options (tls_options, sockopts) are duplicated here and in Mv.Mailer.smtp_config/0
|
||||
# because boot config must be set in this file; the Mailer uses the same logic for
|
||||
# Settings-only config. Keep verify behaviour in sync (see SMTP_VERIFY_PEER below).
|
||||
# per-send at runtime using Mv.Mailer.smtp_config/0 (which uses the same Mv.Smtp.ConfigBuilder).
|
||||
smtp_host_env = System.get_env("SMTP_HOST")
|
||||
|
||||
if smtp_host_env && String.trim(smtp_host_env) != "" do
|
||||
|
|
@ -263,26 +259,15 @@ if config_env() == :prod do
|
|||
|
||||
verify_mode = if smtp_verify_peer, do: :verify_peer, else: :verify_none
|
||||
|
||||
base_smtp_opts = [
|
||||
adapter: Swoosh.Adapters.SMTP,
|
||||
relay: String.trim(smtp_host_env),
|
||||
port: smtp_port_env,
|
||||
username: System.get_env("SMTP_USERNAME"),
|
||||
password: smtp_password_env,
|
||||
ssl: smtp_ssl_mode == "ssl",
|
||||
tls: if(smtp_ssl_mode == "tls", do: :always, else: :never),
|
||||
auth: :always,
|
||||
tls_options: [verify: verify_mode]
|
||||
]
|
||||
|
||||
# Port 465: pass verify in sockopts for initial ssl:connect. Port 587: do not (gen_tcp rejects it).
|
||||
smtp_opts =
|
||||
if smtp_ssl_mode == "ssl" do
|
||||
Keyword.put(base_smtp_opts, :sockopts, verify: verify_mode)
|
||||
else
|
||||
base_smtp_opts
|
||||
end
|
||||
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|
||||
Mv.Smtp.ConfigBuilder.build_opts(
|
||||
host: String.trim(smtp_host_env),
|
||||
port: smtp_port_env,
|
||||
username: System.get_env("SMTP_USERNAME"),
|
||||
password: smtp_password_env,
|
||||
ssl_mode: smtp_ssl_mode,
|
||||
verify_mode: verify_mode
|
||||
)
|
||||
|
||||
config :mv, Mv.Mailer, smtp_opts
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue