Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ config :form, FormWeb.Endpoint,
force_ssl: [rewrite_on: [:x_forwarded_proto]]
#
# Check `Plug.SSL` for all available options in `force_ssl`.

# Email provider
sendgrid_api_key =
System.get_env("SENDGRID_API_KEY") ||
raise """
environment variable SENDGRID_API_KEY is missing.
"""

config :form, FormWeb.Pow.Mailer,
adapter: Swoosh.Adapters.Sendgrid,
api_key: sendgrid_api_key
35 changes: 30 additions & 5 deletions lib/form_web/pow_mailer.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
defmodule FormWeb.Pow.Mailer do
use Pow.Phoenix.Mailer
use Swoosh.Mailer, otp_app: :form

import Swoosh.Email

require Logger

def cast(%{user: user, subject: subject, text: text, html: html, assigns: _assigns}) do
# Build email struct to be used in `process/1`
@impl true
def cast(%{user: user, subject: subject, text: text, html: html}) do
%Swoosh.Email{}
|> to({"", user.email})
|> from({"My App", "myapp@example.com"})
|> subject(subject)
|> html_body(html)
|> text_body(text)

%{to: user.email, subject: subject, text: text, html: html}
# end
end

@impl true
def process(email) do
# Send email

Logger.debug("E-mail sent: #{inspect(email)}")

if Mix.env() == :prod do
Task.start(fn ->
email
|> deliver()
|> log_warnings()
end)

:ok
end
end

defp log_warnings({:error, reason}) do
Logger.warn("Mailer backend failed with: #{inspect(reason)}")
end

defp log_warnings({:ok, response}), do: {:ok, response}
end
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ defmodule Form.MixProject do
{:floki, ">= 0.30.0", only: :test},
{:phoenix_live_dashboard, "~> 0.6"},
{:esbuild, "~> 0.3", runtime: Mix.env() == :dev},
{:swoosh, "~> 1.3"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:gettext, "~> 0.18"},
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},
# auth provider
{:pow, "~> 1.0.26"},
# email provider
{:swoosh, "~> 1.5"},
]
end

Expand Down