Skip to content

Commit

Permalink
feat-contact-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshvardhan1012 authored and raspberri05 committed Dec 13, 2024
1 parent 63ac661 commit 3db2d9d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
64 changes: 64 additions & 0 deletions app/api/contact/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { z } from "zod";
import nodemailer from "nodemailer";

const schema = z.object({
email: z.string().email("Invalid email address"),
message: z.string().min(1, "Message cannot be empty"),
});

export async function POST(req: Request) {
try {
const body = await req.json();
const { email, message } = schema.parse(body);

const transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
port: Number(process.env.PORT),
auth: {
user: process.env.EMAIL,
pass: process.env.PASS,
},
});

const mailOptions = {
to: process.env.EMAIL,
subject: "New Contact Form Submission",
html: `<p>You have received a new message:</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Message:</strong></p>
<p>${message}</p>`,
};

await transporter.sendMail(mailOptions);

return Response.json(
{
success: true,
message: "Email sent successfully",
},
{
status: 200,
},
);
} catch (error) {
if (error instanceof z.ZodError) {
return Response.json(
{
success: false,
error: error.errors.map((err) => ({
path: err.path,
message: err.message,
})),
},
{ status: 400 },
);
}
return Response.json(
{ success: false, error: "Internal Server Error", message: error },
{
status: 500,
},
);
}
}
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"lucide-react": "^0.468.0",
"next": "15.0.4",
"next-themes": "^0.4.4",
"nodemailer": "^6.9.16",
"openai": "^4.76.0",
"postgres": "^3.4.5",
"react": "^19.0.0",
Expand All @@ -46,6 +47,7 @@
},
"devDependencies": {
"@types/node": "^22.10.1",
"@types/nodemailer": "^6.4.17",
"@types/react": "^19.0.1",
"@types/react-dom": "^19.0.1",
"drizzle-kit": "^0.29.1",
Expand Down

0 comments on commit 3db2d9d

Please sign in to comment.