From 81f9302206f5c26e979b8bab4fe40c526d38853b Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 10 Dec 2024 15:22:22 -0500 Subject: [PATCH] Add RFC2822 date header --- lib/philomena/mailer.ex | 28 +++++++++++++++++++++++++++- lib/philomena/users/user_notifier.ex | 16 +--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lib/philomena/mailer.ex b/lib/philomena/mailer.ex index 642f329e..8a9798ca 100644 --- a/lib/philomena/mailer.ex +++ b/lib/philomena/mailer.ex @@ -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 diff --git a/lib/philomena/users/user_notifier.ex b/lib/philomena/users/user_notifier.ex index 395d31e3..81f5329b 100644 --- a/lib/philomena/users/user_notifier.ex +++ b/lib/philomena/users/user_notifier.ex @@ -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. """