Search code examples
node.jsgmail-api

How to reply to threadId using Gmail api in node js?


Here I want to send a reply using Gmail API. For that, I got a successful response while retrieving. Now I have threadId using that threadId I need to send a reply instead of creating a new thread

This is my response for retrieval of mail

{
  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'
} 

And my code for sending reply

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)
                    }
                }

But while using this code a new thread is creating. Need help.


Solution

  • As per documentation:

    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.

    Thus, proving 'test subject' as the subject will not work if this is not the real subject of the email to which you want to reply.

    Also:

    As you can draw the the Resource: Message, you should pass the thread id to the parameter threadId. message_id is not a valid parameter.