import { json, type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/node"; import { Form, useActionData, useLoaderData } from "@remix-run/react"; import { requireAuthLevel } from "~/utils/auth.server"; import { testEmailConnection } from "~/utils/mail.server"; import DashboardLayout from "~/components/DashboardLayout"; import { useState } from "react"; import { prisma } from "~/utils/db.server"; export async function loader({ request }: LoaderFunctionArgs) { // Require auth level 3 to access mail settings const user = await requireAuthLevel(request, 3); const mailSettings = await prisma.mailSettings.findFirst(); return json({ mailSettings, user }); } export async function action({ request }: ActionFunctionArgs) { // Require auth level 3 to modify mail settings await requireAuthLevel(request, 3); const formData = await request.formData(); const action = formData.get("_action"); if (action === "test") { const result = await testEmailConnection(); return json(result); } const host = formData.get("host") as string; const port = parseInt(formData.get("port") as string); const secure = formData.get("secure") === "on"; const username = formData.get("username") as string; const password = formData.get("password") as string; const fromName = formData.get("fromName") as string; const fromEmail = formData.get("fromEmail") as string; if (!host || !port || !username || !password || !fromName || !fromEmail) { return json({ error: "All fields are required" }, { status: 400 }); } try { // Check if settings exist const existingSettings = await prisma.mailSettings.findFirst(); if (existingSettings) { // Update existing settings await prisma.mailSettings.update({ where: { id: existingSettings.id }, data: { host, port, secure, username, password, fromName, fromEmail, }, }); } else { // Create new settings await prisma.mailSettings.create({ data: { host, port, secure, username, password, fromName, fromEmail, }, }); } return json({ success: "Mail settings saved successfully" }); } catch (error) { return json({ error: "Failed to save mail settings" }, { status: 500 }); } } export default function MailSettings() { const { mailSettings, user } = useLoaderData(); const actionData = useActionData(); const [showPassword, setShowPassword] = useState(false); return (

Mail Settings

{/* SMTP Configuration Examples */}

Common SMTP Settings

Gmail

Host: smtp.gmail.com

Port: 587 (TLS) or 465 (SSL)

Note: Use App Password, not regular password

Outlook/Hotmail

Host: smtp-mail.outlook.com

Port: 587 (TLS)

Secure: No (uses STARTTLS)

Yahoo

Host: smtp.mail.yahoo.com

Port: 587 (TLS) or 465 (SSL)

Note: Enable "Less secure apps"

Custom SMTP

Contact your email provider

Port 587 (TLS) is most common

Port 465 (SSL) for secure connections

{actionData?.error && (
{actionData.error}
)} {actionData?.success && (
{actionData.success}
)}
); }