55 lines
1.7 KiB
Elixir
55 lines
1.7 KiB
Elixir
|
|
defmodule BerrypodWeb.EmailSettingsController do
|
||
|
|
@moduledoc """
|
||
|
|
No-JS fallback for email settings form submission.
|
||
|
|
|
||
|
|
With JS enabled, the LiveView handles everything. Without JS,
|
||
|
|
the form POSTs here and we redirect back to the LiveView page.
|
||
|
|
"""
|
||
|
|
use BerrypodWeb, :controller
|
||
|
|
|
||
|
|
alias Berrypod.Mailer
|
||
|
|
|
||
|
|
def update(conn, %{"email" => params}) do
|
||
|
|
selected = params["adapter"]
|
||
|
|
configured = params["configured_adapter"]
|
||
|
|
|
||
|
|
if selected != configured do
|
||
|
|
# User changed adapter radio but config fields are for the old adapter.
|
||
|
|
# Redirect to show the new adapter's config fields.
|
||
|
|
redirect(conn, to: ~p"/admin/settings/email?adapter=#{selected}")
|
||
|
|
else
|
||
|
|
case Mailer.save_config(selected, params, conn.assigns.current_scope.user.email) do
|
||
|
|
{:ok, _adapter_info} ->
|
||
|
|
conn
|
||
|
|
|> put_flash(:info, "Settings saved — send a test email to check it works")
|
||
|
|
|> redirect(to: ~p"/admin/settings/email")
|
||
|
|
|
||
|
|
{:error, field_errors} when is_map(field_errors) ->
|
||
|
|
message = field_errors |> Map.values() |> Enum.join(". ")
|
||
|
|
|
||
|
|
conn
|
||
|
|
|> put_flash(:error, message)
|
||
|
|
|> redirect(to: ~p"/admin/settings/email?adapter=#{selected}")
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def test(conn, _params) do
|
||
|
|
user = conn.assigns.current_scope.user
|
||
|
|
|
||
|
|
case Mailer.send_test_email(user.email, Mailer.from_address()) do
|
||
|
|
{:ok, _} ->
|
||
|
|
Mailer.mark_email_verified()
|
||
|
|
|
||
|
|
conn
|
||
|
|
|> put_flash(:info, "Test email sent to #{user.email}")
|
||
|
|
|> redirect(to: ~p"/admin/settings/email")
|
||
|
|
|
||
|
|
{:error, reason} ->
|
||
|
|
conn
|
||
|
|
|> put_flash(:error, Mailer.friendly_error(reason))
|
||
|
|
|> redirect(to: ~p"/admin/settings/email")
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|