I’m new with NodeJS and I’m trying to send emails with NodeMailer. I have developed my NodeJS code and the POST Request works in ThunderClient, but when I try to send the request from my JS file nothing happens.
What am I doing wrong?:
Thanks for your help!
This is my index.js code:
app.post('/api/send-mail/:nombreEvento/:receptor/:organizador', async function (req, res) {
const { receptor } = req.params
const { organizador } = req.params
const { nombreEvento } = req.params
const cal = new icalendar.iCalendar();
const evento = new icalendar.VEvent();
const inicio = new Date('2024-04-30T10:00:00');
const fin = new Date('2024-04-30T11:00:00');
evento.setSummary(nombreEvento);
evento.setDate(inicio, fin);
evento.setLocation('Google Meet');
evento.addProperty('organizer', `MAILTO:${organizador}`);
cal.addComponent(evento);
const archivoIcs = cal.toString();
const result = await transporter.sendMail({
from: `TRACKWISE ????${process.env.USER_MAIL}`,
to: receptor,
subject: "Llamada Agendada",
text: "Hola!nTienes una nueva llamada agendada",
attachments: [
{
filename: 'evento.ics',
content: archivoIcs
}
]
})
console.log({result})
res.status(200).json({ok: true, message: "Mail Enviado"})
});
And this is my sending mails code in JS:
async function enviarCorreo() {
const nombreEvento = nomEvento.textContent;
const receptor = txtMail.value;
const organizador = mailOrganizador;
const url = 'http://localhost:3000/api/send-mail/' + encodeURIComponent(nombreEvento) + '/' + encodeURIComponent(receptor) + '/' + encodeURIComponent(organizador);
console.log(url);
const requestOptions = {
method: 'POST'
};
try {
const response = await fetch(url, requestOptions);
console.log("Response", response);
if (!response.ok) {
throw new Error('Error en la solicitud: ' + response.status);
}
console.log("Response Status", response.statusText);
} catch (error) {
console.error("Error al enviar el correo:", error);
}
}