-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63ac661
commit 3db2d9d
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters