Add RFC2822 date header

This commit is contained in:
Liam 2024-12-10 15:22:22 -05:00
parent 113c01c7d5
commit 81f9302206
2 changed files with 28 additions and 16 deletions

View file

@ -1,9 +1,35 @@
defmodule Philomena.Mailer do
use Swoosh.Mailer, otp_app: :philomena
alias Swoosh.Email
@spec deliver_later(Swoosh.Email.t()) :: {:ok, Swoosh.Email.t()}
@spec deliver_later(Email.t()) :: {:ok, Email.t()}
def deliver_later(mail) do
Task.Supervisor.start_child(Philomena.AsyncEmailSupervisor, fn -> deliver(mail) end)
{:ok, mail}
end
@spec format_message(Email.t()) :: Email.t()
def format_message(mail) do
mail
|> Email.from({"noreply", mailer_address()})
|> Email.header("Message-ID", rfc2822_message_id())
|> Email.header("Date", rfc2822_date())
end
defp rfc2822_message_id do
id =
:crypto.strong_rand_bytes(16)
|> Base.encode16()
|> String.downcase()
"<#{id}.#{mailer_address()}>"
end
defp rfc2822_date do
Calendar.strftime(DateTime.utc_now(), "%a, %-d %b %Y %H:%M:%S %z")
end
defp mailer_address do
Application.get_env(:philomena, :mailer_address)
end
end

View file

@ -5,27 +5,13 @@ defmodule Philomena.Users.UserNotifier do
defp deliver(to, subject, body) do
Email.new(
to: {to, to},
from: {"noreply", mailer_address()},
subject: subject,
text_body: body
)
|> Email.header("Message-ID", message_id())
|> Mailer.format_message()
|> Mailer.deliver_later()
end
defp message_id do
id =
:crypto.strong_rand_bytes(16)
|> Base.encode16()
|> String.downcase()
"<#{id}.#{mailer_address()}>"
end
defp mailer_address do
Application.get_env(:philomena, :mailer_address)
end
@doc """
Deliver instructions to confirm account.
"""