Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/(pages)/_components/ContactForm/ContactForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export default function ContactForm() {

// Send to API
try {
if (!valid) throw new Error("Invalid input");

const res = await fetch("/api/sendEmail", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -103,7 +105,9 @@ export default function ContactForm() {
setSubject("");
setMessage("");
} catch (error) {
alert("Something went wrong. Try again.");
if (error.message !== "Invalid input") {
alert("Something went wrong. Try again.");
}
} finally {
setLoading(false);
}
Expand Down
48 changes: 37 additions & 11 deletions app/api/sendEmail/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,53 @@ export async function POST(req) {

// Create transporter
const transporter = nodemailer.createTransport({
host: "Zoho",
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
user: process.env.EMAIL_FROM,
pass: process.env.PASSWORD,
},
tls: {
// Do not fail on invalid certs
rejectUnauthorized: false,
},
});

// Send email
const mailOptions = {
from: process.env.EMAIL_USER,
to: "[email protected]", // EDIT WITH ACTUAL CONTACT FORM EMAIL
from: process.env.EMAIL_FROM,
to: process.env.EMAIL_TO, // EDIT WITH ACTUAL CONTACT FORM EMAIL
subject: subject,
text: `
Name: ${firstName} ${lastName}
Email: ${email}
Message: ${message}
`, // Can edit with name/email in body if needed
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form Submission</title>
</head>
<body style="font-family: sans-serif; line-height: 1.6; color: #333; background-color: #f4f4f4; padding: 20px;">
<div style="background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);">
<h1 style="color: #01887a; border-bottom: 2px solid #eee; padding-bottom: 10px; ">Contact Form Submission</h1>
<p style="margin-bottom: 15px;">You have received a new message from your contact form.</p>
<div style="background-color: #f9f9f9; padding: 16px; border-radius: 4px; margin-bottom: 20px; display: flex; flex-direction: column; gap: 16px;">
<div style="margin-bottom: 16px"><strong style="display: block; margin-bottom: 4px;">Name:</strong> ${firstName} ${lastName}</div>
<div style="margin-bottom: 16px"><strong style="display: block; margin-bottom: 4px;">Email:</strong> ${email}</div>
<div style="margin-bottom: 16px"><strong style="display: block; margin-bottom: 4px;">Message:</strong> ${message}</div>
</div>
<div style="margin-top: 20px; text-align: center; color: #777;">
This is an automated email sent from your website's contact form.
</div>
</div>
</body>
</html>
`,
};

await transporter.sendMail(mailOptions);
return NextResponse.json({ message: "Email sent successfully!" });
return NextResponse.json(
{ status: 200 },
{ message: "Email sent successfully!" }
);
} catch (error) {
console.error(error);
return NextResponse.json(
Expand Down