如何在node js中使用Gmail api回复threadId?
How to reply to threadId using Gmail api in node js?
这里我想使用 Gmail 发送回复 API。
为此,我在检索时得到了成功的回应。
现在我有 threadId
使用该 threadId 我需要发送回复而不是创建新线程
这是我对邮件检索的回复
{
id: '178fe5f9cc632096',
threadId: '178fe5f9cc632096',
labelIds: [ 'IMPORTANT', 'CATEGORY_PERSONAL', 'INBOX' ],
snippet: 'it's working --',
payload: {
partId: '',
mimeType: 'multipart/alternative',
filename: '',
headers: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
],
body: { size: 0 },
parts: [ [Object], [Object] ]
},
sizeEstimate: 5218,
historyId: '119777',
internalDate: '1619175369000'
}
还有我发送回复的代码
function makeBody(to, from, subject, message) {
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message
].join('');
var encodedMail = Buffer.from(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail
}
function sendMessage(auth) {
try {
var raw = makeBody('abc@gmail.com', 'xyz@gmail.com', 'test subject', 'workinggggggg...');
const gmail = google.gmail({ version: 'v1', auth });
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw,
message_id: res.data.threadId
}
}, function (err, response) {
if (err) {
return console.log('The API returned an error: ' + err);
}
else
console.log(response)
});
} catch (error) {
console.log(error)
}
}
但是在使用这段代码时,正在创建一个新线程。
需要帮助。
If you're trying to send a reply and want the email to thread, make sure that:
The Subject headers match
The References and In-Reply-To headers follow the RFC 2822 standard.
因此,如果 'test subject'
不是您要回复的电子邮件的真正主题,那么证明 'test subject'
作为主题将不起作用。
另外:
因为你可以绘制Resource: Message,你应该将线程id传递给参数threadId
。 message_id
不是有效参数。
解决方案是将 Message-Id 保留为 In-Reply-To 和 References
您需要像下面这样更新 In-Reply-To 和 References 值
function makeBody(ref, InReply, to, from, subject, message) {
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"References:", ref, "\n" +
"In-Reply-To: ", InReply, "\n" +
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message
].join('');
var encodedMail = Buffer.from(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail
}
const headers = res.data.payload.headers
let subject
let to
let ref
let InReply
headers.forEach(element => {
if (element.name === 'Subject' || element.name === 'subject') {
subject = element.value
}
if (element.name === 'From' || element.name === 'from') {
to = element.value
}
if (element.name === 'Message-ID' || element.name === 'Message-Id') {
ref = element.value
InReply = element.value
}
});
var raw = makeBody(ref, InReply, to, 'example1@gmail.com', subject, 'reply message text');
这里我想使用 Gmail 发送回复 API。 为此,我在检索时得到了成功的回应。 现在我有 threadId 使用该 threadId 我需要发送回复而不是创建新线程
这是我对邮件检索的回复
{
id: '178fe5f9cc632096',
threadId: '178fe5f9cc632096',
labelIds: [ 'IMPORTANT', 'CATEGORY_PERSONAL', 'INBOX' ],
snippet: 'it's working --',
payload: {
partId: '',
mimeType: 'multipart/alternative',
filename: '',
headers: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
],
body: { size: 0 },
parts: [ [Object], [Object] ]
},
sizeEstimate: 5218,
historyId: '119777',
internalDate: '1619175369000'
}
还有我发送回复的代码
function makeBody(to, from, subject, message) {
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message
].join('');
var encodedMail = Buffer.from(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail
}
function sendMessage(auth) {
try {
var raw = makeBody('abc@gmail.com', 'xyz@gmail.com', 'test subject', 'workinggggggg...');
const gmail = google.gmail({ version: 'v1', auth });
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw,
message_id: res.data.threadId
}
}, function (err, response) {
if (err) {
return console.log('The API returned an error: ' + err);
}
else
console.log(response)
});
} catch (error) {
console.log(error)
}
}
但是在使用这段代码时,正在创建一个新线程。 需要帮助。
If you're trying to send a reply and want the email to thread, make sure that:
The Subject headers match The References and In-Reply-To headers follow the RFC 2822 standard.
因此,如果 'test subject'
不是您要回复的电子邮件的真正主题,那么证明 'test subject'
作为主题将不起作用。
另外:
因为你可以绘制Resource: Message,你应该将线程id传递给参数threadId
。 message_id
不是有效参数。
解决方案是将 Message-Id 保留为 In-Reply-To 和 References 您需要像下面这样更新 In-Reply-To 和 References 值
function makeBody(ref, InReply, to, from, subject, message) {
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"References:", ref, "\n" +
"In-Reply-To: ", InReply, "\n" +
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message
].join('');
var encodedMail = Buffer.from(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail
}
const headers = res.data.payload.headers
let subject
let to
let ref
let InReply
headers.forEach(element => {
if (element.name === 'Subject' || element.name === 'subject') {
subject = element.value
}
if (element.name === 'From' || element.name === 'from') {
to = element.value
}
if (element.name === 'Message-ID' || element.name === 'Message-Id') {
ref = element.value
InReply = element.value
}
});
var raw = makeBody(ref, InReply, to, 'example1@gmail.com', subject, 'reply message text');