1

As simple as it may seem, I cannot seem to figure out how to set recipients of a draft with Google's Gmail API. The documentation says that a users.messages Resource object contains a payload object that contains a headers object, and that headers object contains name-value pairs.

// example from google's gmail API documentation
"payload": {
  "partId": string,
  "mimeType": string,
  "filename": string,
  "headers": [
    {
      "name": string,
      "value": string
    }
  ],
  "body": users.messages.attachments Resource,
  "parts": [
    (MessagePart)
  ]
},

It is within these headers I presume you set the "To" part of a draft, since the documentation says

List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.

however, when I make a request that looks something like this

"payload" : {
  "headers" : [
    {
      "name"  : "To",
      "value" : "me"
      // "me" should direct the draft to myself
    }
  ]
}

the To part of the draft is still left empty. Any solution or advice?

2 Answers 2

1

In your request you have this:

"headers" : [ "name" : "To", "value" : "me" ]

"headers" is supposed to be an array of objects but your array contains none.

Instead, it should look like this:

"headers": [ { "name": "To", "value": "me" } ]

Just like their example:

"payload": {
  "partId": string,
  "mimeType": string,
  "filename": string,
  "headers": [
    {
      "name": "To",
      "value": "me"
    }
  ],
  "body": users.messages.attachments Resource,
  "parts": [
    (MessagePart)
  ]
},
1
  • Woops, that was actually a typo. I am writing in my local code that way, but same results when I attempt to create a draft. Nov 7, 2016 at 16:48
1

So, it appears that I was misinterpreting documentation on the Gmail API. When you send a request to drafts.create, you do need to supply is a users.messages Resource, however, not all of it is writable. Only threadId, labelIds, and raw are writable objects. As it turns out, you aren't supposed to use the payload at all to set the To, From, etc. You are supposed to include them in your raw.

My new code looks something like this

let create = (toAddress, subject, content, callback) => {
  gmail.users.drafts.create(
    {
      'userId'  : 'me',
      'resource' : {
        'message' : {
          'raw' : base64.encodeURI(
                    `To:${toAddress}\r\n` + // Who were are sending to
                    `Subject:${subject}\r\n` + // Subject
                    `Date:\r\n` + // Removing timestamp
                    `Message-Id:\r\n` + // Removing message id
                    `From:\r\n` + // Removing from
                    `${content}` // Adding our actual message
                  )
        }
      }
    },
    (err, response) => {
      // Do stuff with response
      callback(err, response);
    }
  )
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.