Skip to content

Commit

Permalink
Move TLS options for system emails to new deliver_system_email!/1 fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
wmnnd committed Oct 11, 2023
1 parent 866bce8 commit ae9bd05
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
13 changes: 2 additions & 11 deletions config/runtime.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Config
require Logger
:ok == Application.ensure_started(:logger)
:ok == Application.ensure_started(:tls_certificate_check)

exit_from_exception = fn exception, message ->
Logger.error(exception.message)
Expand Down Expand Up @@ -85,18 +84,10 @@ if config_env() == :prod do
relay: host,
username: user,
password: password,
from_email: from_email
from_email: from_email,
ssl: ssl?
]
|> put_if_not_empty.(:port, port)
|> then(fn config ->
if ssl? do
config
|> Keyword.put(:ssl, true)
|> Keyword.put(:sockopts, :tls_certificate_check.options(host))
else
config
end
end)
end

config(:keila, Keila.Mailer, config)
Expand Down
2 changes: 1 addition & 1 deletion lib/keila/auth/emails.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Keila.Auth.Emails do
def send!(email, params) do
email
|> build(params)
|> Keila.Mailer.deliver!()
|> Keila.Mailer.deliver_system_email!()
end

@spec build(:activate, %{url: String.t(), user: Keila.Auth.User.t()}) :: term() | no_return()
Expand Down
20 changes: 20 additions & 0 deletions lib/keila/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ defmodule Keila.Mailer do
alias Keila.Mailings.Sender
alias Keila.Mailings.SharedSender

@doc """
Delivers an email using the configured system mailer.
"""
@spec deliver_system_email!(Swoosh.Email.t()) :: term()
def deliver_system_email!(email) do
config =
Application.get_env(:keila, __MODULE__)
|> maybe_put_tls_opts()

deliver!(email, config)
end

defp maybe_put_tls_opts(config) do
if Keyword.get(config, :ssl) do
Keyword.put(config, :sockopts, :tls_certificate_check.options(config[:relay]))
else
config
end
end

@doc """
Delivers an email using a given sender.
"""
Expand Down

0 comments on commit ae9bd05

Please sign in to comment.