I was trying to implement a form that uses Next.js & Nodemailer to send emails, and it works just fine on Chrome & Bing. However, I am unable to send emails using Firefox.
This is the button that inputs the form data to the backend (in pages/index.js):
<input type="submit" onClick={onSubmit} disabled={!values.name || !values.subject || !values.email || !values.message} />
This is the function that is called onClick (in pages/index.js):
const onSubmit = async () => {
try {
await sendContactForm(values);
} catch (error) {
console.log(error);
}
};
This is the function that awaits the form data (in lib/api.js):
export const sendContactForm = async (data) =>
fetch("/api/contact", {
method: "POST",
body: JSON.stringify(data),
headers: { "Content-Type": "application/json", Accept: "application/json" },
}).then((res) => {
if (!res.ok) throw new Error("Failed to send message");
return res.json();
});
This is the function that sends the email (the nodemailer configuration was done in a different file) (in pages/api/contact.js):
import { mailOptions, transporter } from "config/nodemailer.js";
const handler = async (req, res) => {
if (req.method === "POST") {
const data = req.body;
if (!data || !data.name || !data.email || !data.subject || !data.message) {
return res.status(400).send({ message: "Bad request" });
}
try {
await transporter.sendMail({
...mailOptions,
...generateEmailContent(data), //This is a function that returns the user inputted form data as a string
subject: data.subject,
});
return res.status(200).json({ success: true });
} catch (err) {
console.log(err);
return res.status(400).json({ message: err.message });
}
}
return res.status(400).json({ message: "Bad request" });
};
Here is the GitHub repository as well for more context: https://github.com/Jacques-Bisset/nodemailer-testing
Here are some things I tried in the Firefox settings:
- Disabled HTTPS-only mode
- Turned browser security mode to “standard” instead of “strict”
- Unmarked all checkboxes in the “Deceptive Content and Dangerous Software Protection” settings
None of these configurations seem to have done anything
As I said before, this code works perfectly fine in other web browsers – it is in Firefox specifically where no email is sent for whatever reason.
However, when I click the submit button on Firefox, this message pops up in the terminal:
warn - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
This also only occurs for Firefox and not other web browsers.
Thank you for any help in advance.