feat(mailer): send mail asynchronously

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2021-08-20 03:00:17 +05:30
parent d734e8ad02
commit caf3535e74
No known key found for this signature in database
GPG Key ID: ED5C54FBBB920E51
3 changed files with 70 additions and 57 deletions

View File

@ -16,32 +16,6 @@ config :ketbin, KetbinWeb.Endpoint,
# Do not print debug messages in production # Do not print debug messages in production
config :logger, level: :info 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 # ## SSL Support
# #
# To get SSL working, you will need to add the `https` key # To get SSL working, you will need to add the `https` key

View File

@ -33,6 +33,31 @@ config :ketbin, KetbinWeb.Endpoint,
secret_key_base: secret_key_base, secret_key_base: secret_key_base,
server: true 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+) # ## Using releases (Elixir v1.9+)
# #
# If you are doing OTP releases, you need to instruct Phoenix # If you are doing OTP releases, you need to instruct Phoenix

View File

@ -6,16 +6,18 @@ defmodule Ketbin.Accounts.UserNotifier do
# * Swoosh - https://hexdocs.pm/swoosh # * Swoosh - https://hexdocs.pm/swoosh
# * Bamboo - https://hexdocs.pm/bamboo # * Bamboo - https://hexdocs.pm/bamboo
# #
defp deliver(to, body) do defp deliver(to, body, subject) do
require Logger require Logger
Logger.debug(body) Logger.debug(body)
new() Task.start(fn ->
|> to(to) new()
|> from("noreply@katb.in") |> to(to)
|> subject("Password reset requested") |> from({"Katbin", "noreply@katb.in"})
|> text_body(body) |> subject(subject)
|> Ketbin.Mailer.deliver() |> text_body(body)
|> Ketbin.Mailer.deliver()
end)
{:ok, %{to: to, body: body}} {:ok, %{to: to, body: body}}
end end
@ -24,59 +26,71 @@ defmodule Ketbin.Accounts.UserNotifier do
Deliver instructions to confirm account. Deliver instructions to confirm account.
""" """
def deliver_confirmation_instructions(user, url) do 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 end
@doc """ @doc """
Deliver instructions to reset a user password. Deliver instructions to reset a user password.
""" """
def deliver_reset_password_instructions(user, url) do 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 end
@doc """ @doc """
Deliver instructions to update a user email. Deliver instructions to update a user email.
""" """
def deliver_update_email_instructions(user, url) do 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
end end