Okay, so I’m trying to set up a simple email form on my website, the user enters a message, and then an email is sent with said message. Locally, everything works fine, but on the live version of my website, I get a 502 error. I’ve already looked around the forums and added various things to my configuration but it still doesn’t work. Is it possible that my env variables aren’t being loaded on the server? What am I missing here?
If out of curiosity, my domain name is https://rohitkisto.dev, contact form is at the bottom.
Here are some screen shots of my configuration.
This is part of my frontend, where the serverless function gets called.
const sendMessage = async ({ message }: FormInputs) => {
const { status } = await fetch("./.netlify/functions/send-message", {
method: "POST",
body: JSON.stringify({
message
})
});
if (status !== 200) {
throw new Error(`Error ${status} occured while sending message.`);
}
reset();
};
Here’s my serverless function.
import type { Handler } from '@netlify/functions';
import fetch from "node-fetch";
const handler: Handler = async ({ body }) => {
if (body === null) {
return {
statusCode: 400,
body: JSON.stringify("Payload required")
};
}
const { message } = JSON.parse(body) as {
message: string
}
const { status, statusText } = await fetch(
`./.netlify/functions/emails/subscribed`,
{
headers: {
"netlify-emails-secret": process.env.NETLIFY_EMAILS_SECRET as string,
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": process.env.NETLIFY_URL as string
},
method: "POST",
body: JSON.stringify({
from: "There is an email here",
to: "There is an email here",
subject: "New Message",
parameters: {
message
},
}),
}
);
return {
statusCode: status,
body: JSON.stringify(status === 200 ? "Message sent!" : `An error occured sending email!\n${statusText}`)
}
}
export { handler };
My netlify.toml.
[build]
command = 'npm run build'
functions = 'netlify/functions'
publish = 'dist'
[[plugins]]
package = "@netlify/plugin-emails"
If there’s anything else I can provide please let me know