feat(mailer): send mail asynchronously
Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
parent
d734e8ad02
commit
caf3535e74
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
Task.start(fn ->
|
||||
new()
|
||||
|> to(to)
|
||||
|> from("noreply@katb.in")
|
||||
|> subject("Password reset requested")
|
||||
|> from({"Katbin", "noreply@katb.in"})
|
||||
|> subject(subject)
|
||||
|> text_body(body)
|
||||
|> Ketbin.Mailer.deliver()
|
||||
end)
|
||||
|
||||
{:ok, %{to: to, body: body}}
|
||||
end
|
||||
@ -24,7 +26,9 @@ defmodule Ketbin.Accounts.UserNotifier do
|
||||
Deliver instructions to confirm account.
|
||||
"""
|
||||
def deliver_confirmation_instructions(user, url) do
|
||||
deliver(user.email, """
|
||||
deliver(
|
||||
user.email,
|
||||
"""
|
||||
|
||||
==============================
|
||||
|
||||
@ -37,14 +41,18 @@ defmodule Ketbin.Accounts.UserNotifier do
|
||||
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,
|
||||
"""
|
||||
|
||||
==============================
|
||||
|
||||
@ -57,14 +65,18 @@ defmodule Ketbin.Accounts.UserNotifier do
|
||||
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,
|
||||
"""
|
||||
|
||||
==============================
|
||||
|
||||
@ -77,6 +89,8 @@ defmodule Ketbin.Accounts.UserNotifier do
|
||||
If you didn't request this change, please ignore this.
|
||||
|
||||
==============================
|
||||
""")
|
||||
""",
|
||||
"Email update requested"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user