From caf3535e745ff249531230c31aefba6e4076f4b0 Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Fri, 20 Aug 2021 03:00:17 +0530 Subject: [PATCH] feat(mailer): send mail asynchronously Signed-off-by: SphericalKat --- config/prod.exs | 26 ---------- config/releases.exs | 25 +++++++++ lib/ketbin/accounts/user_notifier.ex | 76 ++++++++++++++++------------ 3 files changed, 70 insertions(+), 57 deletions(-) diff --git a/config/prod.exs b/config/prod.exs index a30f6ff..3453716 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -16,32 +16,6 @@ config :ketbin, KetbinWeb.Endpoint, # Do not print debug messages in production config :logger, level: :info -smtp_relay = - System.get_env("SWOOSH_SMTP_RELAY") || - raise """ - environment variable SWOOSH_SMTP_RELAY is missing. - """ -username = - System.get_env("SWOOSH_USERNAME") || - raise """ - environment variable SWOOSH_USERNAME is missing. - """ -password = - System.get_env("SWOOSH_PASSWORD") || - raise """ - environment variable SWOOSH_PASSWORD is missing. - """ - -# configure mailer -config :ketbin, Ketbin.Mailer, - adapter: Swoosh.Adapters.SMTP, - relay: smtp_relay, - username: username, - password: password, - tls: :always, - auth: :always, - port: 587 - # ## SSL Support # # To get SSL working, you will need to add the `https` key diff --git a/config/releases.exs b/config/releases.exs index 35c4ada..d82088a 100644 --- a/config/releases.exs +++ b/config/releases.exs @@ -33,6 +33,31 @@ config :ketbin, KetbinWeb.Endpoint, secret_key_base: secret_key_base, server: true + smtp_relay = + System.get_env("SWOOSH_SMTP_RELAY") || + raise """ + environment variable SWOOSH_SMTP_RELAY is missing. + """ + username = + System.get_env("SWOOSH_USERNAME") || + raise """ + environment variable SWOOSH_USERNAME is missing. + """ + password = + System.get_env("SWOOSH_PASSWORD") || + raise """ + environment variable SWOOSH_PASSWORD is missing. + """ + + # configure mailer + config :ketbin, Ketbin.Mailer, + adapter: Swoosh.Adapters.SMTP, + relay: smtp_relay, + username: username, + password: password, + tls: :always, + auth: :always, + port: 587 # ## Using releases (Elixir v1.9+) # # If you are doing OTP releases, you need to instruct Phoenix diff --git a/lib/ketbin/accounts/user_notifier.ex b/lib/ketbin/accounts/user_notifier.ex index cb5987d..f123591 100644 --- a/lib/ketbin/accounts/user_notifier.ex +++ b/lib/ketbin/accounts/user_notifier.ex @@ -6,16 +6,18 @@ defmodule Ketbin.Accounts.UserNotifier do # * Swoosh - https://hexdocs.pm/swoosh # * Bamboo - https://hexdocs.pm/bamboo # - defp deliver(to, body) do + defp deliver(to, body, subject) do require Logger Logger.debug(body) - new() - |> to(to) - |> from("noreply@katb.in") - |> subject("Password reset requested") - |> text_body(body) - |> Ketbin.Mailer.deliver() + Task.start(fn -> + new() + |> to(to) + |> from({"Katbin", "noreply@katb.in"}) + |> subject(subject) + |> text_body(body) + |> Ketbin.Mailer.deliver() + end) {:ok, %{to: to, body: body}} end @@ -24,59 +26,71 @@ defmodule Ketbin.Accounts.UserNotifier do Deliver instructions to confirm account. """ def deliver_confirmation_instructions(user, url) do - deliver(user.email, """ + deliver( + user.email, + """ - ============================== + ============================== - Hi #{user.email}, + Hi #{user.email}, - You can confirm your account by visiting the URL below: + You can confirm your account by visiting the URL below: - #{url} + #{url} - If you didn't create an account with us, please ignore this. + If you didn't create an account with us, please ignore this. - ============================== - """) + ============================== + """, + "Account confirmation" + ) end @doc """ Deliver instructions to reset a user password. """ def deliver_reset_password_instructions(user, url) do - deliver(user.email, """ + deliver( + user.email, + """ - ============================== + ============================== - Hi #{user.email}, + Hi #{user.email}, - You can reset your password by visiting the URL below: + You can reset your password by visiting the URL below: - #{url} + #{url} - If you didn't request this change, please ignore this. + If you didn't request this change, please ignore this. - ============================== - """) + ============================== + """, + "Password reset requested" + ) end @doc """ Deliver instructions to update a user email. """ def deliver_update_email_instructions(user, url) do - deliver(user.email, """ + deliver( + user.email, + """ - ============================== + ============================== - Hi #{user.email}, + Hi #{user.email}, - You can change your email by visiting the URL below: + You can change your email by visiting the URL below: - #{url} + #{url} - If you didn't request this change, please ignore this. + If you didn't request this change, please ignore this. - ============================== - """) + ============================== + """, + "Email update requested" + ) end end