Hello,
I have encountered a problem with my app after deployed it, I’m using nodemailer package to send an email from a website. Everything works fine in development and I’m able to receive mail from the site, but in a production environment, i got a 404 error status on my request
the netlify site name is https://fanciful-cendol-d532d8.netlify.app
Front:
const contact = {
initContact:function(){
contact.listener();
},
dataForm:{
contactForm : document.querySelector('.contact-form'),
email : document.querySelector(".contact-email"),
message : document.querySelector(".contact-text"),
emailState : document.getElementById("input-hidden"),
server_url : "/.netlify/functions/sendmail"
},
listener: function(){
contact.dataForm.contactForm.addEventListener('submit', contact.actionListener.handleSubmit)
},
actionListener: {
handleSubmit : (e) => {
e.preventDefault();
fetch(contact.dataForm.server_url,{
method:"POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(
{
datas: {
email: contact.dataForm.email.value,
message: contact.dataForm.message.value
}
}
)
}
)
.then(res => {
if(res.ok){
res.json()
.then(data => {
console.log(data)
console.log(res)
window.alert("Votre email a bien été envoyé")
contact.dataForm.email.value = '';
contact.dataForm.message.value = '';
})
}
else{
console.log(res)
console.log("There is a problem with nodemailer")
contact.errorResponse()
}
})
.catch(function(error) {
console.log('There is a problem with the fetch operation: ' + error.message);
contact.errorResponse()
});
}
},
errorResponse: function(){
window.alert("Une erreur est survenue, merci d'utiliser le lien Linkedin")
contact.dataForm.email.value = '';
contact.dataForm.message.value = '';
}
}
document.addEventListener('DOMContentLoaded', contact.initContact);
i have just changed the server_url from my localhost to
server_url : "/.netlify/functions/sendmail"
i found informations on this post, but not really sure its appropriate to my code, problem coub be from here
https://answers.netlify.com/t/netlify-function-for-sending-mail-with-nodemailer-doesnt-work-in-production/41148
back:
app.post('/',(req,res) => {
try{
const transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 465,
secure: true,
service:'gmail',
auth:{
user: process.env.GMAIL_USER,
pass: process.env.PASSWORD,
}
})
console.log(req.body.datas.email)
const mailOptions = {
from: req.body.datas.email,
to: process.env.GMAIL_USER,
subject: "email from my Portfolio",
html: `You got a message from
Email : ${req.body.datas.email} <br>
Message: ${req.body.datas.message}`,
}
transporter.sendMail(mailOptions, (error, info) => {
res.status(200)
res.json({email:"email sent"})
});
}catch(error){
res.status(500)
res.json({message: error})
}
})
As i said, in developement i got no probleme, i recieve the email with all the informations, after some researchs and differents trials, with no success, i am asking some advices,
Please let me know if any other information needs in order to help solve my problem.
Thank you.