Reputation: 11
I am trying to replay to email hru gmail api as described: https://developers.google.com/gmail/api/guides/sending
I am successfully replaying to email however my goal is to have replay with original email thread like you would do thru gmail UI. It is replaying even changing email subject to have "Re:" prefix however it is not threading original email for some reason. Any help apprepiate.
Google documentation says this:
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.
I have taken references, subject, inReplyTo and threadId variables from original message i am replaying to by executing query thru gmail.users.messages.get.
I also have tried to creade draft but results are the same (draft without original email thread content).
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.');
}
}
);
});
}
Upvotes: 1
Views: 354
Reputation: 1
I FOUND THE SOLUTION !
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);
Upvotes: 0