next-enterprise/app/api/routes/route.ts

35 lines
1,014 B
TypeScript
Raw Normal View History

2024-04-22 00:01:07 +05:30
import { mailOptions, transporter } from "./mailTransporter"
2024-04-22 05:13:51 +00:00
import { replaceMergeTags, stripHTMLTags } from "app/mail/sliceHtml"
2024-04-22 00:01:07 +05:30
import { NextResponse } from "next/server"
2024-04-22 05:13:51 +00:00
import fs from "fs"
import path from "path"
2024-04-22 00:01:07 +05:30
2024-04-22 05:13:51 +00:00
export async function POST(req: any) {
2024-04-22 00:01:07 +05:30
const data = await req.json()
2024-04-22 05:13:51 +00:00
const htmlFilePath = path.join(process.cwd(), "app/mail", "contactForm.html")
let htmlContent: string
try {
htmlContent = fs.readFileSync(htmlFilePath, "utf8")
} catch (err) {
console.error("Error reading HTML file: ", err)
return
}
htmlContent = replaceMergeTags(data, htmlContent)
const plainTextContent = stripHTMLTags(htmlContent)
2024-04-22 00:01:07 +05:30
try {
await transporter.sendMail({
...mailOptions,
2024-04-22 05:13:51 +00:00
subject: `Contact Inquiry - VizoleLabs Web`,
text: plainTextContent,
html: htmlContent,
2024-04-22 00:01:07 +05:30
})
return NextResponse.json({ success: true })
} catch (error: any) {
console.error(error)
return NextResponse.json({ error: error.message }, { status: error.status })
}
}