const onFinish = (values: any) => {
const finalFormData = { ...formData, ...values };
console.log("Final form data:", finalFormData);
axios
.post("http://localhost:1337/api/applicants", { data: finalFormData })
.then((response) => {
message.success("A candidatura foi submetida com sucesso");
// After successful submission, send an email notification
axios
.post("/api/sendEmail", { finalFormData })
.then((emailResponse) => {
message.success("Email sent successfully");
})
.catch((emailError) => {
console.error("Error sending email:", emailError);
message.error("Error sending email");
});
})
.catch((error) => {
console.error("Error submitting the form:", error);
message.error("Houve um erro ao submeter a candidatura");
});
};
-----
import nodemailer from "nodemailer";
export default async function handler(req: any, res: any) {
if (req.method !== "POST") {
res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
// Assuming you have SMTP_EMAIL and SMTP_PASSWORD set in your environment variables
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.SMTP_EMAIL,
pass: process.env.SMTP_PASSWORD,
},
});
const { finalFormData } = req.body;
const mailOptions = {
from: process.env.SMTP_EMAIL, // Use the email from environment variable
to: "[email protected]",
subject: "New Application Submission",
text: `You've received a new application: ${JSON.stringify(finalFormData)}`,
};
try {
// Verify transporter
await transporter.verify();
// Send email`enter code here`
const info = await transporter.sendMail(mailOptions);
console.log("Email sent: " + info.response);
res.status(200).send("Email sent successfully");
} catch (error) {
console.error("Error sending email:", error);
res.status(500).send("Error sending email");
}
}