通过 Gmail API 发送回复电子邮件时重播中缺少线程

问题描述 投票:0回答:1

我正在尝试重播电子邮件 hru gmail api,如下所述:https://developers.google.com/gmail/api/guides/sending

我成功重播到电子邮件,但我的目标是像通过 gmail UI 那样重播原始电子邮件线程。 它正在重播,甚至将电子邮件主题更改为带有“Re:”前缀,但由于某种原因,它没有线程化原始电子邮件。 任何帮助表示赞赏。

Google 文档是这样说的:

如果您尝试发送回复并希望电子邮件被线程化,请确保:

  1. 主题标题匹配

  2. ReferencesIn-Reply-To 标头遵循 RFC 2822 标准。

我通过执行查询gmail.users.messages.get,从我正在重播的原始消息中获取了referencessubjectinReplyTothreadId变量。

我也尝试过创建草稿,但结果是相同的(草稿没有原始电子邮件线程内容)。

public async send() { const from = '[email protected]'; const to = '[email protected]'; const subject = 'test 01'; const text = 'bbbbbbbbbbb'; const inReplyTo='<CAHmEgS2VvD8i-B-PrHRXA0Egwed4RcW7f3wNbMYiCx5YqYhYsw@mail.gmail.com>'; const references='<CAHmEgS2VvD8i-B-PrHRXA0Egwed4RcW7f3wNbMYiCx5YqYhYsw@mail.gmail.com>'; const threadId = '17c26a0c9045c813'; const buildMessage = () => new Promise<string>((resolve, reject) => { const message = new MailComposer({ from: from, to: to, subject: subject, text: text, textEncoding: 'base64', headers: { 'In-Reply-To': inReplyTo, 'References': references, } }) message.compile().build((err, msg) => { if (err) { reject(err) } const encodedMessage = Buffer.from(msg) .toString('base64') .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=+$/, '') resolve(encodedMessage) }) }) const encodedMessage = await buildMessage() const auth = await this.authorize(); const gmail = google.gmail({ version: 'v1', auth }); return new Promise((resolve, reject) => { return gmail.users.messages.send( { userId: 'me', requestBody: { raw: encodedMessage, threadId: threadId } }, (err: any, res: any) => { if (err) reject(err); resolve(res); const messages = res.data.messages; if (messages.length) { messages.forEach((message: { id: any; threadId: any; }) => { console.log(`${message.id}_${message.threadId}`); }); } else { console.error('No messages found.'); } } ); }); }
    
javascript typescript google-api protractor gmail-api
1个回答
0
投票
我找到了解决方案!

const threadId = originalMessage.threadId; const headers = originalMessage.payload.headers; const from = headers.find((header) => header.name === 'From').value; const to = headers.find((header) => header.name === 'To').value; const inReplyTo = headers.find( (header) => header.name === 'Message-ID' ).value; const subject = headers.find( (header) => header.name === 'Subject' ).value; const body = 'ALLLLLEZZZZ'; // eslint-disable-next-line no-undef const utf8Subject = `=?utf-8?B?${Buffer.from(subject).toString( 'base64' )}?=`; const messageParts = [ 'From: ' + to, 'To: ' + from, 'In-Reply-To: ' + inReplyTo, 'References: ' + inReplyTo, 'Content-Type: text/html; charset=utf-8', 'MIME-Version: 1.0', 'Subject: ' + utf8Subject, '', '' + body, ]; // on join tous ces bouts de phrases const message = messageParts.join('\r\n'); // The body needs to be base64url encoded. // eslint-disable-next-line no-undef const encodedMessage = Buffer.from(message) .toString('base64') .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=+$/, ''); const res = await m.gmail.users.messages.send({ userId: 'me', requestBody: { raw: encodedMessage, threadId: threadId, }, }); console.log('Message send'); console.log(res.data);
    
© www.soinside.com 2019 - 2024. All rights reserved.