Gmail Rest API json to send message

Viewed 217

I'm having trouble sending a json request to the gmail api. I am able to access the scoop https://www.googleapis.com/auth/gmail.send which allows me to access the sending methods.

In the google documentation they state that the rest needs to have this data below. https://developers.google.com/gmail/api/reference/rest/v1/users.messages

But I'm not getting to fill them, what to put in them? Has anyone ever used the api via rest sending just a json?

I researched here on the stack but I didn't find anyone with this difficulty. And on google documentation has no examples.

{
  "id": string,
  "threadId": string,
  "labelIds": [
    string
  ],
  "snippet": string,
  "historyId": string,
  "internalDate": string,
  "payload": {
    object (MessagePart)
  },
  "sizeEstimate": integer,
  "raw": string
}
1 Answers

Message should be encoded as base64. you can generate one by pasting this code into node REPL, or into online node compiler like this one https://replit.com/languages/nodejs

function createMessageJson(){
    const messages = [
        'From: NAME <foo@email.com>',
        'To: Name <foobar@email.com>',
        'Content-Type: text/html; charset=utf-8',
        'MIME-Version: 1.0',
        'Subject: Re: SUBJECT',
        '',
        'BODY_TEXT',
        '',
    ];


    function encodedMessage (){
        return Buffer.from(messages.join('\n'))
            .toString('base64')
            .replace(/\+/g, '-')
            .replace(/\//g, '_')
            .replace(/=+$/, '');
    }


  return JSON.stringify({
            raw: encodedMessage()
    });
}

console.log(createMessageJson())
Related