From b2492a387086528598da36f11e53569c5bdb164c Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 17 Aug 2023 10:35:03 +0200 Subject: [PATCH] fix(backend): handle email not being sent when resending registration instructions Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/user.ex | 3 +++ lib/web/email/mailer.ex | 10 +++++++++- lib/web/email/user.ex | 14 ++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index 73d85e0ee..e08f413ce 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -302,6 +302,9 @@ defmodule Mobilizon.GraphQL.Resolvers.User do {:error, :invalid_email} -> {:error, dgettext("errors", "This email doesn't seem to be valid")} + {:error, :failed_sending_mail} -> + {:error, dgettext("errors", "Couldn't send an email. Internal error.")} + {:error, :email_too_soon} -> {:error, dgettext( diff --git a/lib/web/email/mailer.ex b/lib/web/email/mailer.ex index 2f78477a2..90632214b 100644 --- a/lib/web/email/mailer.ex +++ b/lib/web/email/mailer.ex @@ -4,10 +4,18 @@ defmodule Mobilizon.Web.Email.Mailer do """ use Swoosh.Mailer, otp_app: :mobilizon alias Mobilizon.Service.ErrorReporting.Sentry + require Logger @spec send_email(Swoosh.Email.t()) :: {:ok, term} | {:error, term} def send_email(email) do - Mobilizon.Web.Email.Mailer.deliver(email) + Logger.debug( + "Mailer options #{inspect(Keyword.drop(Application.get_env(:mobilizon, Mobilizon.Web.Email.Mailer), [:tls_options]))}" + ) + + Logger.debug("Sending mail, #{inspect(email)}") + res = Mobilizon.Web.Email.Mailer.deliver(email) + Logger.debug("Return from sending mail #{inspect(res)}") + res rescue error -> Sentry.capture_exception(error, diff --git a/lib/web/email/user.ex b/lib/web/email/user.ex index d0278d7da..6967f055b 100644 --- a/lib/web/email/user.ex +++ b/lib/web/email/user.ex @@ -91,13 +91,19 @@ defmodule Mobilizon.Web.Email.User do Users.update_user(user, %{ "confirmation_sent_at" => DateTime.utc_now() |> DateTime.truncate(:second) }) do - send_confirmation_email(user, locale) - Logger.info("Sent confirmation email again to #{user.email}") - {:ok, user.email} + case send_confirmation_email(user, locale) do + {:ok, _} -> + Logger.info("Sent confirmation email again to #{user.email}") + {:ok, user.email} + + {:error, err} -> + Logger.error("Failed sending email to #{user.email}. #{inspect(err)}") + {:error, :failed_sending_mail} + end end end - @spec send_confirmation_email(User.t(), String.t()) :: Swoosh.Email.t() + @spec send_confirmation_email(User.t(), String.t()) :: {:ok, term} | {:error, term} def send_confirmation_email(%User{} = user, locale \\ "en") do user |> Email.User.confirmation_email(locale)