63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
export async function onRequestPost(context) {
|
|
try {
|
|
const { name, email, subject, message } = await context.request.json();
|
|
|
|
// Validate inputs
|
|
if (!name || !email || !subject || !message) {
|
|
return new Response(
|
|
JSON.stringify({ error: 'All fields are required' }),
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
// Send email using Resend
|
|
const response = await fetch('https://api.resend.com/emails', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${context.env.RESEND_API_KEY}`,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
from: {
|
|
email: email,
|
|
name: name
|
|
},
|
|
to: [
|
|
{
|
|
email: "daniel@laforceit.com",
|
|
name: "Daniel LaForce"
|
|
}
|
|
],
|
|
subject: `ArgoBox Contact Form: ${subject}`,
|
|
html: `
|
|
<h3>New Contact Message on ArgoBox.com</h3>
|
|
<p><strong>Name:</strong> ${name}</p>
|
|
<p><strong>Email:</strong> ${email}</p>
|
|
<p><strong>Subject:</strong> ${subject}</p>
|
|
<h4>Message:</h4>
|
|
<p>${message}</p>
|
|
`,
|
|
reply_to: [
|
|
{
|
|
email,
|
|
name
|
|
}
|
|
]
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to send email');
|
|
}
|
|
|
|
return new Response(
|
|
JSON.stringify({ message: 'Message sent successfully!' }),
|
|
{ status: 200, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
} catch (error) {
|
|
return new Response(
|
|
JSON.stringify({ error: 'Failed to send message' }),
|
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
}
|