Skip to content

API Route - Contact Form

quote.ts

export const prerender = false;
import type { APIRoute } from "astro";
import * as postmark from "postmark";
import mjml2html from "mjml";
export const POST: APIRoute = async ({ request, redirect }) => {
const client = new postmark.ServerClient(import.meta.env.POSTMARK_TOKEN);
const data = await request.json();
const location = data.location;
const deliveryZip = data.deliveryZip;
const firstName = data.firstName;
const lastName = data.lastName;
const email = data.email;
const phone = data.phone;
const message = data.message;
// Validate required fields
if (
!email ||
!phone ||
!location ||
!deliveryZip ||
!firstName ||
!lastName
) {
return new Response(
JSON.stringify({
status: "400 Bad Request",
message: "Email and phone are required fields.",
}),
{ status: 400 }
);
}
// Prepare email content for BREM
const { html: emailBody } = mjml2html(`
<mjml>
<mj-body background-color="#f4f4f4" font-family="Arial, sans-serif">
<mj-section background-color="#ffffff" padding-top="20px" padding-bottom="20px">
<mj-column width="100%">
<mj-image src="https://bremstorage.com/assets/images/logo.png" alt="BREM Logo" width="150px" align="center" />
<mj-divider border-color="#dddddd" border-width="1px" />
</mj-column>
</mj-section>
<mj-section background-color="#ffffff" padding="30px">
<mj-column width="100%">
<mj-text font-size="22px" color="#333333" font-weight="bold" align="center">
Thank you for contacting BREM Storage Solutions!
</mj-text>
<mj-text font-size="16px" color="#555555" line-height="1.6" align="center">
Hi ${firstName} ${lastName},<br/>
We’ve received your inquiry, and we’re excited to assist you. Here are the details you submitted:
</mj-text>
<mj-divider border-color="#dddddd" border-width="1px" />
<mj-text font-size="16px" color="#333333" line-height="1.6">
<strong>Email:</strong> ${email}<br/>
<strong>Phone:</strong> ${phone}<br/>
<strong>Location:</strong> ${location}<br/>
<strong>Delivery Zip:</strong> ${deliveryZip}
${message ? `<strong>Message:</strong> ${message}` : ''}
</mj-text>
<mj-text font-size="16px" color="#555555" line-height="1.6" align="center">
A member of our team will contact you soon. Thank you for choosing BREM Storage Solutions!
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`);
// Send email
try {
await client.sendEmail({
From: "support@bremstorage.com",
To: `${email}`,
Cc: "",
Subject: "BREM Storage Solutions Inquiry",
HtmlBody: emailBody,
MessageStream: "outbound",
});
} catch (error) {
console.error("Error sending email:", error);
return new Response(
JSON.stringify({
status: "500 Internal Server Error",
message: "An error occurred while sending the email.",
}),
{ status: 500 }
);
}
// Redirect to the thank-you page
let redirectUrl = "/thank-you?";
return redirect(redirectUrl, 302);
};