63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
"use server";
|
|
|
|
import nodemailer from "nodemailer";
|
|
|
|
interface ContactFormData {
|
|
fullName: string;
|
|
email: string;
|
|
subject: string;
|
|
message: string;
|
|
}
|
|
|
|
export async function sendEmailAction(data: ContactFormData) {
|
|
const { fullName, email, subject, message } = data;
|
|
|
|
if (!fullName || !email || !message) {
|
|
return { success: false, error: "Name, email, and message are required." };
|
|
}
|
|
|
|
let password = process.env.SMTP_PASSWORD || "";
|
|
if (password.startsWith('"') && password.endsWith('"')) {
|
|
password = password.slice(1, -1);
|
|
}
|
|
// Remove spaces just in case
|
|
password = password.replace(/\s+/g, "");
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.SMTP_HOST,
|
|
port: Number(process.env.SMTP_PORT) || 465,
|
|
secure: true,
|
|
auth: {
|
|
user: process.env.SMTP_USER,
|
|
pass: password,
|
|
},
|
|
});
|
|
|
|
try {
|
|
await transporter.sendMail({
|
|
from: `"${fullName}" <${process.env.SMTP_USER}>`,
|
|
to: process.env.CONTACT_RECEIVER,
|
|
replyTo: email,
|
|
subject: `New Contact Form Submission: ${subject || "No Subject"}`,
|
|
html: `
|
|
<div style="font-family: sans-serif; color: #333;">
|
|
<h2>New Contact Form Submission</h2>
|
|
<p><strong>Name:</strong> ${fullName}</p>
|
|
<p><strong>Email:</strong> ${email}</p>
|
|
<p><strong>Subject:</strong> ${subject}</p>
|
|
<p><strong>Message:</strong></p>
|
|
<blockquote style="background: #f9f9f9; padding: 15px; border-left: 4px solid #ccc;">
|
|
${message.replace(/\n/g, "<br>")}
|
|
</blockquote>
|
|
</div>
|
|
`,
|
|
});
|
|
|
|
return { success: true };
|
|
} catch (error: any) {
|
|
console.error("Email send error:", error);
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
return { success: false, error: errorMessage || "Failed to send the email. Please try again later." };
|
|
}
|
|
}
|