diff --git a/config/runtime.exs b/config/runtime.exs index 0a34ad2..8ec2e01 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -21,7 +21,8 @@ if System.get_env("PHX_SERVER") do end config :comfycamp, - jwt_secret: System.get_env("JWT_SECRET") + jwt_secret: System.get_env("JWT_SECRET"), + smtp_from: System.get_env("SMTP_FROM") || "admin@comfycamp.space" if config_env() == :prod do database_url = @@ -108,8 +109,8 @@ if config_env() == :prod do relay: System.get_env("SMTP_RELAY"), username: System.get_env("SMTP_USERNAME"), password: System.get_env("SMTP_PASSWORD"), - ssl: System.get_env("SMTP_SSL") == "true", - tls: :if_available, + ssl: true, + tls: :always, auth: :always, port: Integer.parse(System.get_env("SMTP_PORT") || "465") diff --git a/lib/comfycamp/accounts/user_notifier.ex b/lib/comfycamp/accounts/user_notifier.ex index fcab32c..6cf8f93 100644 --- a/lib/comfycamp/accounts/user_notifier.ex +++ b/lib/comfycamp/accounts/user_notifier.ex @@ -8,7 +8,7 @@ defmodule Comfycamp.Accounts.UserNotifier do email = new() |> to(recipient) - |> from({"Comfycamp", "contact@example.com"}) + |> from({"Comfycamp", Application.fetch_env!(:comfycamp, :smtp_from)}) |> subject(subject) |> text_body(body) diff --git a/lib/comfycamp/mailer.ex b/lib/comfycamp/mailer.ex index e648933..cb2cb7c 100644 --- a/lib/comfycamp/mailer.ex +++ b/lib/comfycamp/mailer.ex @@ -1,3 +1,27 @@ defmodule Comfycamp.Mailer do use Swoosh.Mailer, otp_app: :comfycamp + + alias Swoosh.Email + alias Comfycamp.Mailer + + def send_test_email(addr) do + email = + Email.new() + |> Email.to(addr) + |> Email.from({"Comfycamp", Application.fetch_env!(:comfycamp, :smtp_from)}) + |> Email.subject("Test email") + |> Email.text_body(""" + ============================== + + Hi, + + Email is working fine! + + ============================== + """) + + with {:ok, _metadata} <- Mailer.deliver(email) do + {:ok, email} + end + end end diff --git a/lib/comfycamp_web/components/layouts/admin.html.heex b/lib/comfycamp_web/components/layouts/admin.html.heex index 9d34ee4..2543e24 100644 --- a/lib/comfycamp_web/components/layouts/admin.html.heex +++ b/lib/comfycamp_web/components/layouts/admin.html.heex @@ -26,6 +26,11 @@ OpenID +
  • + <.link href={~p"/admin/email"}> + Email + +
  • <%= @inner_content %> diff --git a/lib/comfycamp_web/controllers/admin_page_controller.ex b/lib/comfycamp_web/controllers/admin_page_controller.ex index dbab5fa..3017f24 100644 --- a/lib/comfycamp_web/controllers/admin_page_controller.ex +++ b/lib/comfycamp_web/controllers/admin_page_controller.ex @@ -20,4 +20,18 @@ defmodule ComfycampWeb.AdminPageController do |> put_layout(html: :admin) |> render(:home, page_title: "Админка") end + + def email_overview(conn, _params) do + conn + |> put_layout(html: :admin) + |> render(:email_overview, page_title: "Email - Админка") + end + + def send_email(conn, %{"addr" => addr}) do + Comfycamp.Mailer.send_test_email(addr) + + conn + |> put_flash(:info, "Письмо отправлено") + |> redirect(to: ~p"/admin/email") + end end diff --git a/lib/comfycamp_web/controllers/admin_page_html.ex b/lib/comfycamp_web/controllers/admin_page_html.ex index d80e16a..ad1bb25 100644 --- a/lib/comfycamp_web/controllers/admin_page_html.ex +++ b/lib/comfycamp_web/controllers/admin_page_html.ex @@ -30,4 +30,16 @@ defmodule ComfycampWeb.AdminPageHTML do """ end + + def email_overview(assigns) do + ~H""" +
    + <.header>Проверка email + <.form for={nil} action={~p"/admin/send_email"} method="POST"> + <.input label="Тестовый адрес" name="addr" value="" type="email" /> + <.button type="submit">Отправить + +
    + """ + end end diff --git a/lib/comfycamp_web/router.ex b/lib/comfycamp_web/router.ex index b910c2b..cddb2ab 100644 --- a/lib/comfycamp_web/router.ex +++ b/lib/comfycamp_web/router.ex @@ -114,6 +114,10 @@ defmodule ComfycampWeb.Router do get "/", AdminPageController, :home get "/services", AdminPageController, :services + + get "/email", AdminPageController, :email_overview + post "/send_email", AdminPageController, :send_email + resources "/notes", NotesEditorController resources "/users", UserEditorController, only: [:index, :show] resources "/oidc_apps", OIDCAppController