Update email settings, add email tester

This commit is contained in:
Ivan R. 2024-10-17 23:31:27 +05:00
parent 401009bfdb
commit 88f5c52c50
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
7 changed files with 64 additions and 4 deletions

View file

@ -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")

View file

@ -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)

View file

@ -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

View file

@ -26,6 +26,11 @@
OpenID
</.link>
</li>
<li>
<.link href={~p"/admin/email"}>
Email
</.link>
</li>
</ul>
<%= @inner_content %>
</div>

View file

@ -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

View file

@ -30,4 +30,16 @@ defmodule ComfycampWeb.AdminPageHTML do
</div>
"""
end
def email_overview(assigns) do
~H"""
<div>
<.header>Проверка email</.header>
<.form for={nil} action={~p"/admin/send_email"} method="POST">
<.input label="Тестовый адрес" name="addr" value="" type="email" />
<.button type="submit">Отправить</.button>
</.form>
</div>
"""
end
end

View file

@ -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