feat: full site build — Project/Melody schema (Option A), admin CRUD, public sections, uploads, email+SMTP, internal analytics, legal pages, docs
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import { render } from "@react-email/render";
|
||||
import ContactMessageEmail, { ContactConfirmationEmail } from "@/components/emails/contact-message";
|
||||
|
||||
type ContactEmailData = { name: string; email: string; message: string };
|
||||
|
||||
function getTransporter() {
|
||||
const port = Number(process.env.SMTP_PORT || 587);
|
||||
return nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port,
|
||||
secure: process.env.SMTP_SECURE === "true" || port === 465,
|
||||
auth: process.env.SMTP_USER && process.env.SMTP_PASSWORD
|
||||
? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD }
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
function getEmailConfig() {
|
||||
const from = process.env.SMTP_FROM;
|
||||
const to = process.env.SMTP_TO || process.env.NEXT_PUBLIC_CONTACT_EMAIL;
|
||||
if (!process.env.SMTP_HOST || !from || !to) throw new Error("SMTP email configuration is incomplete.");
|
||||
return { from, to };
|
||||
}
|
||||
|
||||
function getPlainText({ name, email, message }: ContactEmailData) {
|
||||
return [`New contact message`, ``, `Name: ${name}`, `Email: ${email}`, ``, message].join("\n");
|
||||
}
|
||||
|
||||
function getConfirmationText(name: string, siteUrl: string) {
|
||||
return [`Hi ${name},`, ``, `Your message was received. I will get back to you as soon as possible.`, ``, siteUrl].join("\n");
|
||||
}
|
||||
|
||||
export async function sendContactMessageEmail(data: ContactEmailData) {
|
||||
const { from, to } = getEmailConfig();
|
||||
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000";
|
||||
const html = await render(ContactMessageEmail({ ...data, siteUrl }));
|
||||
const transporter = getTransporter();
|
||||
await transporter.sendMail({
|
||||
from,
|
||||
to,
|
||||
replyTo: data.email,
|
||||
subject: `New contact message from ${data.name}`,
|
||||
text: getPlainText(data),
|
||||
html,
|
||||
});
|
||||
const confirmationHtml = await render(ContactConfirmationEmail({ name: data.name, siteUrl }));
|
||||
await transporter.sendMail({
|
||||
from,
|
||||
to: data.email,
|
||||
subject: "Thanks for contacting Diyaa",
|
||||
text: getConfirmationText(data.name, siteUrl),
|
||||
html: confirmationHtml,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user