我正在尝试使用node.js 中的nodemailer 重播电子邮件,但我不能。 它总是生成一个新线程而不是回复原始电子邮件。
这是我的代码:
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);
}
}
首先我发送一封没有 idMessage 的电子邮件,然后我得到:
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]>
目前运行良好,但是当我使用 messageId 调用该函数时:
sendEmail('<[email protected]>');
我明白了:
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]>
但是正在生成一个新线程而不是回复
您似乎在使用 Node.js 中的 Nodemailer 时遇到了一些障碍 - 您的电子邮件没有被完全识别为对原始线程的回复。别担心,你并不孤单;这是一个很常见的问题。
这里的罪魁祸首可能是 Message-ID 和 In-Reply-To 标头。这些小细节通常在电子邮件客户端如何将消息组织到线程中发挥重要作用。
现在,当您回复电子邮件线程时,通过在 mailOptions 对象中设置 inReplyTo 和引用字段,您已经迈出了良好的一步。但是,尽管提供了原始消息 ID (idMessage),您的电子邮件似乎仍在单独飞行,而不是加入原始对话。
要解决此问题,请仔细检查这些标头是否设置正确。它们需要与您要回复的原始电子邮件的消息 ID 相匹配。这样,电子邮件客户端就可以将各个点连接起来,并将所有内容保持在同一个整洁的线程中。
let mailOptions;
if (idMessage) {
mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_2,
subject: 'test',
html: `<p>Test:</p>`,
inReplyTo: idMessage, // Set the In-Reply-To header to the original message ID
references: idMessage, // Set the References header to the original message ID
};