I’m trying to replay an email with nodemailer in node.js, but I can’t.
It always generate a new thread instead of reply to the original email.
This is my code:
async function sendEmail(idMessage) {
try {
const transporter = nodemailer.createTransport({
host: 'mail.test.es',
port: 25,
secure: false,
ignoreTLS: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
});
let mailOptions;
if (idMessage) {
console.log("idMessage:",idMessage)
mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_2,
subject: 'test',
html: `<p>Test:</p>`,
inReplyTo: idMessage,
references: `[${idMessage}]`,
};
}else{
mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_2,
subject: 'test',
html: `<p>Test:</p>`
};
}
console.log("mailOptions",mailOptions)
const info = await transporter.sendMail(mailOptions);
console.log('Response:', info.response);
console.log('info:', info);
console.log('id:', info.messageId);
} catch (error) {
console.log('Error', error);
}
}
first I send an email without idMessage, and I get this:
mailOptions {
from: '[email protected]',
to: '[email protected]',
subject: 'test',
html: '<p>Test:</p>'
}
Response: 250 OK id=1s7AyX-0000000Dhau-4Ab4
info: {
accepted: [ '[email protected]' ],
rejected: [],
ehlo: [
'STARTTLS',
'SIZE 52428800',
'8BITMIME',
'AUTH PLAIN LOGIN',
'SIZE 52428800',
'HELP'
],
envelopeTime: 202,
messageTime: 110,
messageSize: 280,
response: '250 OK id=1s7AyX-0000000Dhau-4Ab4',
envelope: { from: '[email protected]', to: [ '[email protected]' ] },
messageId: '<[email protected]>'
}
id: <[email protected]>
At the moment is working well, but when I call the function with the messageId:
sendEmail('<[email protected]>');
I get this:
idMessage: <[email protected]>
mailOptions {
from: '[email protected]',
to: '[email protected]',
subject: 'test',
html: '<p>Test:</p>',
inReplyTo: '<[email protected]>',
references: '[<[email protected]>]'
}
Response: 250 OK id=1s7B2d-0000000DiGI-2H7V
info: {
accepted: [ '[email protected]' ],
rejected: [],
ehlo: [
'STARTTLS',
'SIZE 52428800',
'8BITMIME',
'AUTH PLAIN LOGIN',
'SIZE 52428800',
'HELP'
],
envelopeTime: 208,
messageTime: 112,
messageSize: 409,
response: '250 OK id=1s7B2d-0000000DiGI-2H7V',
envelope: { from: '[email protected]', to: [ '[email protected]' ] },
messageId: '<[email protected]>'
}
id: <[email protected]>
But is generating a new thread instead of a reply