Skip to content

Commit ae9bd05

Browse files
committed
Move TLS options for system emails to new deliver_system_email!/1 function
1 parent 866bce8 commit ae9bd05

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Diff for: config/runtime.exs

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Config
22
require Logger
33
:ok == Application.ensure_started(:logger)
4-
:ok == Application.ensure_started(:tls_certificate_check)
54

65
exit_from_exception = fn exception, message ->
76
Logger.error(exception.message)
@@ -85,18 +84,10 @@ if config_env() == :prod do
8584
relay: host,
8685
username: user,
8786
password: password,
88-
from_email: from_email
87+
from_email: from_email,
88+
ssl: ssl?
8989
]
9090
|> put_if_not_empty.(:port, port)
91-
|> then(fn config ->
92-
if ssl? do
93-
config
94-
|> Keyword.put(:ssl, true)
95-
|> Keyword.put(:sockopts, :tls_certificate_check.options(host))
96-
else
97-
config
98-
end
99-
end)
10091
end
10192

10293
config(:keila, Keila.Mailer, config)

Diff for: lib/keila/auth/emails.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Keila.Auth.Emails do
77
def send!(email, params) do
88
email
99
|> build(params)
10-
|> Keila.Mailer.deliver!()
10+
|> Keila.Mailer.deliver_system_email!()
1111
end
1212

1313
@spec build(:activate, %{url: String.t(), user: Keila.Auth.User.t()}) :: term() | no_return()

Diff for: lib/keila/mailer.ex

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ defmodule Keila.Mailer do
33
alias Keila.Mailings.Sender
44
alias Keila.Mailings.SharedSender
55

6+
@doc """
7+
Delivers an email using the configured system mailer.
8+
"""
9+
@spec deliver_system_email!(Swoosh.Email.t()) :: term()
10+
def deliver_system_email!(email) do
11+
config =
12+
Application.get_env(:keila, __MODULE__)
13+
|> maybe_put_tls_opts()
14+
15+
deliver!(email, config)
16+
end
17+
18+
defp maybe_put_tls_opts(config) do
19+
if Keyword.get(config, :ssl) do
20+
Keyword.put(config, :sockopts, :tls_certificate_check.options(config[:relay]))
21+
else
22+
config
23+
end
24+
end
25+
626
@doc """
727
Delivers an email using a given sender.
828
"""

0 commit comments

Comments
 (0)