google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

gmail.users.drafts.create - 'In-Reply-To' and 'References' Headers not setting

Open e1um opened this issue 6 years ago • 6 comments

Environment details

  • OS: macOS Catalina 10.15.1
  • Node.js version: 11.14.0
  • npm version: 6.13.4
  • googleapis version:46.0.0

Issue

Gmail threads fine from the sender's account but doesn't thread in the recipient's gmail account.

Steps to reproduce

  1. First tried adding the headers in the payload object
const msgId = // Some gmail message id

const mimeDraftEncodedMessage = base64url(`To:[email protected]\r\n` +
      `From:[email protected]\r\n` +
      `Subject:sample subject\r\n` +
      `Content-Type: text/html; charset=utf-8\r\n` +
      `MIME-Version: 1.0\r\n\r\ntest message`;)

const params = {
      userId: 'me',
      requestBody: {
        message: {
          raw: mimeDraftEncodedMessage,
          threadId,
          payload: {
            headers: [
              { name: 'In-Reply-To', value: `<${msgId}>` },
              { name: 'References', value: `<${msgId}>` },
            ],
          },
        },
      },
    }

gmail.users.drafts.create(params)
  1. Then tried adding the headers to the MIME encoded message itself
const msgId = // Some gmail message id

const mimeDraftEncodedMessage = base64url(`To:[email protected]\r\n` +
      `From:[email protected]\r\n` +
      `Subject:sample subject\r\n` +
      `In-Reply-To:${msgId}\r\n` +
      `References:${msgId}\r\n` +
      `Content-Type: text/html; charset=utf-8\r\n` +
      `MIME-Version: 1.0\r\n\r\ntest message`;)

const params = {
      userId: 'me',
      requestBody: {
        message: {
          raw: mimeDraftEncodedMessage,
          threadId,
        },
      },
    }

gmail.users.drafts.create(params)

Any help would be greatly appreciated. Thanks!

e1um avatar Jan 16 '20 04:01 e1um

@e1um have you checked the raw email content in the recipients account to verify the headers are present?

bcoe avatar Jan 17 '20 00:01 bcoe

@bcoe yea I checked through the 'show original' and the headers aren't there. Is there something I'm missing so that the headers go through? It's definitely in the MIME message before I encode it. Thanks!

e1um avatar Jan 17 '20 01:01 e1um

what is the value of threadId, in the docs I notice:

The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard

I recommend:

  1. making sure threadId matches this format.
  2. try setting threadId without the headers.
  3. if this doesn't work, try dropping the <, and > from the headers, I would guess that the API inserts this for you.

bcoe avatar Jan 17 '20 01:01 bcoe

@bcoe

  1. I set the threadId to the threadId that gmail gives in response to gmail.users.drafts.create(params) . I believe this is how I'm able to get threading on the sender side. I couldn't find a reference to 'threadId' in RFC 2822.

What I gathered from RFC 2822 is that the References and In-Reply-To headers should be set to the messageId that the new draft is replying to (to help with threading?). What I'm unsure of is if the messageId that gmail provides is the same as the global messageId?

  1. threadId without the headers doesn't do it either. That's why I tried adding the headers to the payload.

  2. Yea tried it without the < and >

Really appreciate the help! I feel like I've tried everything and still can't move the needle on this...

e1um avatar Jan 17 '20 02:01 e1um

Thanks, I finally figured this out. This post was super helpful - https://stackoverflow.com/questions/32589476/how-to-send-a-reply-with-gmail-api/32591614#32591614

Probably not your job, but I think the gmail api guides should make a better distinction between gmail 'msgId' and the 'Message-Id' from the headers.

e1um avatar Jan 20 '20 02:01 e1um

@e1um awesome, I'm glad you figured this out :+1:

bcoe avatar Jan 20 '20 21:01 bcoe