我正在使用nodemailer发送电子邮件。但我想发送邮件回复(开始一个新线程)。我尝试使用以下代码发送邮件回复
var nodemailer = require('nodemailer');
const subject = 'Hey I am test';
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '*******',
pass: '*******'
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject,
text: 'Body',
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log(info.messageId);
const threadOptions = {
from: '[email protected]',
to: '[email protected]',
subject:'Re: ' + subject,
text: 'This is reply',
inReplyTo: info.messageId,
references: [info.messageId]
};
transporter.sendMail(threadOptions);
}
});
但是它正在作为新邮件发送。在doc中提到使用inReplyTo和references字段将启动一个新线程。但它不起作用
任何一点帮助都会非常感激
Subject
应与发送邮件的Subject
相同。当回复邮件的主题与您已回复的邮件相同时,回复邮件将在之前发送的邮件的线程中
您必须定义您要回复的电子邮件帐户的凭据
抱歉我的英语不好
经过大量研究,我认为这个链接可以提供帮助:https://stackoverflow.com/a/2429134/8270395。 也看看这个:给自己发送一封电子邮件,但让我回复另一封电子邮件。
希望这有帮助。
回复电子邮件的主题应保持不变,与原始电子邮件的主题一致。如果回复的主题与要回复的电子邮件的主题相匹配,则回复将正确地串入上一个电子邮件对话中。
const nodemailer = require("nodemailer");
// Create a Nodemailer transporter
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "[email protected]",
pass: "your-password",
},
});
// Email content
let mailOptions = {
from: "[email protected]", // sender address
to: "[email protected]", // list of receivers
subject: "Testing Nodemailer with advanced fields",
text: "Hello, this is a test email!",
sender: "[email protected]", // Appears in Sender: field
replyTo: "[email protected]", // Appears in Reply-To: field
inReplyTo: "<[email protected]>",
references: ["<[email protected]>", "<[email protected]>"],
envelope: {
from: "[email protected]",
to: "[email protected]",
},
};
// Send email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("Error occurred:", error.message);
return;
}
console.log("Email sent successfully!");
});