Skip to content

Commit 93229e8

Browse files
authored
Improve SMTP config (#197)
Drops Mua in favour of the SMTP Swoosh adapter and adjusts the config accordingly. The lock file has also been cleaned up by running `mix deps.unlock --unused`.
1 parent 374535c commit 93229e8

File tree

4 files changed

+96
-87
lines changed

4 files changed

+96
-87
lines changed

.env.sample

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# === Basic Phoenix configuration ===
2+
13
BASE_URL=http://localhost:4000
24
# SAME_SITE_COOKIE=Lax
35
# SECURE_COOKIE=false
@@ -6,51 +8,75 @@ DATABASE_URL=postgres://claper:claper@db:5432/claper
68
SECRET_KEY_BASE=0LZiQBLw4WvqPlz4cz8RsHJlxNiSqM9B48y4ChyJ5v1oA0L/TPIqRjQNdPZN3iEG # Generate with `mix phx.gen.secret`
79
# ⚠️ Don't use this exact value for SECRET_KEY_BASE or someone would be able to sign a cookie with user_id=1 and log in as the admin!
810

9-
# Storage configuration
11+
12+
# === Storage configuration ===
1013

1114
PRESENTATION_STORAGE=local
1215
PRESENTATION_STORAGE_DIR=/app/uploads
13-
#MAX_FILE_SIZE_MB=15
16+
# MAX_FILE_SIZE_MB=15
17+
18+
# == Standard AWS environment variables
19+
20+
# S3_ACCESS_KEY_ID=xxx
21+
# S3_SECRET_ACCESS_KEY=xxx
22+
# S3_REGION=eu-west-3
23+
# S3_BUCKET=xxx
1424

15-
# The standard AWS environment variables
16-
#S3_ACCESS_KEY_ID=xxx
17-
#S3_SECRET_ACCESS_KEY=xxx
18-
#S3_REGION=eu-west-3
19-
#S3_BUCKET=xxx
25+
# == If you're using an alternative S3-compatible service, port optional
2026

21-
# If you're using an alternative S3-compatible service, port optional
22-
#S3_SCHEME=https://
23-
#S3_HOST=www.example.com
24-
#S3_PORT=443
27+
# S3_SCHEME=https://
28+
# S3_HOST=www.example.com
29+
# S3_PORT=443
2530

26-
# If the public S3-compatible URL is different from the one used to write data
27-
#S3_PUBLIC_URL=https://www.example.com
31+
# == If the public S3-compatible URL is different from the one used to write data
2832

29-
# Mail configuration
33+
# S3_PUBLIC_URL=https://www.example.com
3034

31-
MAIL_TRANSPORT=local
35+
36+
# === Mail configuration ===
37+
38+
MAIL_TRANSPORT=local # smtp or postmark, anything else uses the local adapter
3239
3340
MAIL_FROM_NAME=Claper
3441

35-
#SMTP_RELAY=xx.example.com
36-
37-
#SMTP_PASSWORD=xxx
38-
#SMTP_PORT=465
42+
# == Use the following if MAIL_TRANSPORT=smtp
43+
44+
# SMTP_RELAY=smtp.example.com
45+
# SMTP_PORT=465
46+
# SMTP_RETRIES=1
47+
# SMTP_NO_MX_LOOKUPS=false
48+
49+
# SMTP_AUTH=always # if_available, always or never
50+
# SMTP_USERNAME=username
51+
# SMTP_PASSWORD=xxx
52+
53+
# SMTP_SSL=false
54+
# SMTP_TLS=if_available # if_available, always or never
55+
# SMTP_SSL_DEPTH=2
56+
# SMTP_SSL_SERVER=*.example.com
57+
58+
# == Use the following if MAIL_TRANSPORT=postmark
59+
60+
# POSTMARK_API_KEY=xxx
61+
62+
# == Dev mailbox
63+
64+
# ENABLE_MAILBOX_ROUTE=false
65+
# MAILBOX_USER=admin
66+
# MAILBOX_PASSWORD=admin
67+
3968

40-
#ENABLE_MAILBOX_ROUTE=false
41-
#MAILBOX_USER=admin
42-
#MAILBOX_PASSWORD=admin
69+
# === Claper configuration ===
4370

44-
# Claper configuration
71+
# ENABLE_ACCOUNT_CREATION=true
72+
# EMAIL_CONFIRMATION=true
73+
# ALLOW_UNLINK_EXTERNAL_PROVIDER=false
74+
# LOGOUT_REDIRECT_URL=https://google.com
75+
# GS_JPG_RESOLUTION=300x300
76+
# LANGUAGES=en,fr,es,it,nl,de
4577

46-
#ENABLE_ACCOUNT_CREATION=true
47-
#EMAIL_CONFIRMATION=true
48-
#ALLOW_UNLINK_EXTERNAL_PROVIDER=false
49-
#LOGOUT_REDIRECT_URL=https://google.com
50-
#GS_JPG_RESOLUTION=300x300
51-
#LANGUAGES=en,fr,es,it,nl,de
5278

53-
# OIDC configuration
79+
# === OIDC configuration ===
5480

5581
# OIDC_PROVIDER_NAME="OpenID"
5682
# OIDC_ISSUER=https://my-idp.example/application/o/claper/

config/runtime.exs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ email_confirmation =
7676
pool_size = get_int_from_path_or_env(config_dir, "POOL_SIZE", 10)
7777
queue_target = get_int_from_path_or_env(config_dir, "QUEUE_TARGET", 5_000)
7878

79-
mail_transport = get_var_from_path_or_env(config_dir, "MAIL_TRANSPORT", "local")
80-
81-
smtp_relay = get_var_from_path_or_env(config_dir, "SMTP_RELAY", nil)
82-
smtp_username = get_var_from_path_or_env(config_dir, "SMTP_USERNAME", nil)
83-
smtp_password = get_var_from_path_or_env(config_dir, "SMTP_PASSWORD", nil)
84-
smtp_ssl = get_var_from_path_or_env(config_dir, "SMTP_SSL", "true") |> String.to_existing_atom()
85-
smtp_tls = get_var_from_path_or_env(config_dir, "SMTP_TLS", "always")
86-
smtp_auth = get_var_from_path_or_env(config_dir, "SMTP_AUTH", "always")
87-
smtp_port = get_int_from_path_or_env(config_dir, "SMTP_PORT", 25)
88-
8979
storage = get_var_from_path_or_env(config_dir, "PRESENTATION_STORAGE", "local")
9080
if storage not in ["local", "s3"], do: raise("Invalid PRESENTATION_STORAGE value #{storage}")
9181

@@ -222,26 +212,45 @@ config :claper, ClaperWeb.MailboxGuard,
222212
get_var_from_path_or_env(config_dir, "ENABLE_MAILBOX_ROUTE", "false")
223213
|> String.to_existing_atom()
224214

225-
case mail_transport do
215+
case get_var_from_path_or_env(config_dir, "MAIL_TRANSPORT", "local") do
226216
"smtp" ->
217+
relay = get_var_from_path_or_env(config_dir, "SMTP_RELAY", nil)
218+
ssl = get_var_from_path_or_env(config_dir, "SMTP_SSL", "true")
219+
depth = get_int_from_path_or_env(config_dir, "SMTP_SSL_DEPTH", 2)
220+
221+
server =
222+
get_var_from_path_or_env(config_dir, "SMTP_SSL_SERVER", relay)
223+
|> to_charlist()
224+
227225
config :claper, Claper.Mailer,
228-
adapter: Swoosh.Adapters.Mua,
229-
relay: smtp_relay,
230-
port: smtp_port
231-
232-
cond do
233-
smtp_username && smtp_password ->
234-
config :claper, Claper.Mailer, auth: [username: smtp_username, password: smtp_password]
235-
236-
smtp_username || smtp_password ->
237-
raise ArgumentError, """
238-
Both SMTP_USERNAME and SMTP_PASSWORD must be set for SMTP authentication.
239-
Please provide values for both environment variables.
240-
"""
241-
242-
true ->
243-
nil
244-
end
226+
adapter: Swoosh.Adapters.SMTP,
227+
relay: relay,
228+
port: get_int_from_path_or_env(config_dir, "SMTP_PORT", 465),
229+
auth: get_var_from_path_or_env(config_dir, "SMTP_AUTH", "always"),
230+
username: get_var_from_path_or_env(config_dir, "SMTP_USERNAME", ""),
231+
password: get_var_from_path_or_env(config_dir, "SMTP_PASSWORD", ""),
232+
retries: get_int_from_path_or_env(config_dir, "SMTP_RETRIES", 1),
233+
no_mx_lookups: get_var_from_path_or_env(config_dir, "SMTP_NO_MX_LOOKUPS", "false"),
234+
ssl: ssl,
235+
sockopts:
236+
if(ssl == "true",
237+
do: [
238+
versions: [:"tlsv1.3", :"tlsv1.2"],
239+
verify: :verify_peer,
240+
cacerts: :public_key.cacerts_get(),
241+
depth: depth,
242+
server_name_indication: server
243+
],
244+
else: []
245+
),
246+
tls: get_var_from_path_or_env(config_dir, "SMTP_TLS", "if_available"),
247+
tls_options: [
248+
versions: [:"tlsv1.3", :"tlsv1.2"],
249+
verify: :verify_peer,
250+
cacerts: :public_key.cacerts_get(),
251+
depth: depth,
252+
server_name_indication: server
253+
]
245254

246255
config :swoosh, :api_client, false
247256

@@ -250,7 +259,7 @@ case mail_transport do
250259
adapter: Swoosh.Adapters.Postmark,
251260
api_key: get_var_from_path_or_env(config_dir, "POSTMARK_API_KEY", nil)
252261

253-
config :swoosh, :api_client, Swoosh.ApiClient.Hackney
262+
config :swoosh, :api_client, Swoosh.ApiClient.Finch
254263

255264
_ ->
256265
config :claper, Claper.Mailer, adapter: Swoosh.Adapters.Local
@@ -267,5 +276,3 @@ if s3_scheme && s3_host do
267276
config :ex_aws,
268277
s3: [scheme: s3_scheme, host: s3_host, port: s3_port]
269278
end
270-
271-
config :swoosh, :api_client, Swoosh.ApiClient.Finch

mix.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ defmodule Claper.MixProject do
9595
{:esbuild, "~> 0.10", runtime: Mix.env() == :dev},
9696
{:dart_sass, "~> 0.7", runtime: Mix.env() == :dev},
9797
{:swoosh, "~> 1.19"},
98+
{:gen_smtp, "~> 1.3"},
9899
{:finch, "~> 0.19"},
99100
{:telemetry_metrics, "~> 1.1"},
100101
{:telemetry_poller, "~> 1.2"},
@@ -105,7 +106,6 @@ defmodule Claper.MixProject do
105106
{:hashids, "~> 2.1"},
106107
{:libcluster, "~> 3.5"},
107108
{:porcelain, "~> 2.0"},
108-
{:hackney, "~> 1.24"},
109109
{:csv, "~> 3.2"},
110110
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
111111
{:joken, "~> 2.6"},
@@ -114,8 +114,6 @@ defmodule Claper.MixProject do
114114
{:uuid, "~> 1.1"},
115115
{:oidcc, "~> 3.5"},
116116
{:oban, "~> 2.19"},
117-
{:mua, "~> 0.2"},
118-
{:mail, "~> 0.5"},
119117
{:tailwind, "~> 0.3", runtime: Mix.env() == :dev}
120118
]
121119
end

0 commit comments

Comments
 (0)