Reply to a thread (mail) with attachment being sen

2019-08-18 05:51发布

问题:

I am trying to send reply to a thread (mail) with attachment using gmail api. But it is being sent as a new mail in senders account, but in receiver account it is seen in same thread. This mail should be in same thread for both sender and receiver sides as well.

I am using node js and request-promise module for achieving this. Here is my piece of code. Please have a look and let me know where I am wrong.

let from = req.body.email;
    let mailString = '';
    for (let i in req.files) {
      mailString += '--boundary_mail1\n'
      mailString += `Content-Type: ${req.files[i].mimetype}\n`
      mailString += `Content-Disposition: attachment; filename="${req.files[i].filename}"\n`
      mailString += "Content-Transfer-Encoding: base64\n\n"
      mailString += `${fs.readFileSync(req.files[i].path).toString('base64')}` + '\n'
    }
    let raw = [
      'MIME-Version: 1.0\n',
      "to: ", reply.mailContent.to, "\n",
      "from: ", from, "\n",
      "threadId: ", reply.mailContent.id, "\n",
      "References: ", reply.mailContent.references, "\n",
      "In-Reply-To: ", reply.mailContent.messageId, "\n",
      "subject: ", reply.mailContent.subject, "\n",
      "Content-Type: multipart/mixed; boundary=boundary_mail1\n\n",

      "--boundary_mail1\n",
      "Content-Type: multipart/alternative; boundary=boundary_mail2\n\n",

      "--boundary_mail2\n",
      "Content-Type: text/html; charset=UTF-8\n",
      "Content-Transfer-Encoding: quoted-printable\n\n",
      req.body.message = req.body.message ? req.body.message : '', "\n\n",
      "--boundary_mail2--\n",
      mailString,
      '--boundary_mail1--',      
    ].join('');
    const id = 'me';
    let options = {
      url: "https://www.googleapis.com/upload/gmail/v1/users/" + id + "/messages/send?uploadType=multipart",
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${user.options.access_token}`,
        'Content-Type': 'message/rfc822'
      },
      body: raw
    };
    await request(options).then(async b => {
        return res.apiOk();
      }).catch(err => {
        return res.apiError(err);
      })
    }).catch(err => {
      return res.apiError("Something went wrong!");
    })