10

I try the use the - try it of Google REST API - Users.messages: send .

There is there a required parameter - raw -

The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied.

I checked about RFC 2822 format and seems it should displayed as the sample here , then I encoded it the base64URL with this encoder and paste it the raw field of the try it and I get - Invalid value for ByteString: http://ostermiller.org/calc/encode.html .

Can you provide me a correct RFC 2822 format and its corresponding base64URL which it would work in the above try it ?

URL87
  • 10,667
  • 35
  • 107
  • 174
  • 1
    Check this link http://stackoverflow.com/questions/24460422/how-to-send-a-message-successfully-using-the-new-gmail-rest-api which has all information about RFC 2822 specification – SGC Mar 18 '15 at 16:46
  • [example using node and formatted json](https://stackoverflow.com/questions/69441660/gmail-rest-api-json-to-send-message/70909067#70909067) – Gal Morad Jan 29 '22 at 21:54
  • Indeed, Google API is not just Base64, as it contains the following characters "-_" as detected with https://base64.guru/tools/validator – profimedica Feb 12 '22 at 01:02

2 Answers2

19

An example mail could look like this:

From: sender@gmail.com
To: receiver@gmail.com
Subject: Subject Text

The message text goes here

Open up the Developer Tools in your browser and Base64 encode it and replace all + with -, replace all / with _, and remove the trailing = to make it URL-safe:

btoa(
  "From: sender@gmail.com\r\n" +
  "To: receiver@gmail.com\r\n" +
  "Subject: Subject Text\r\n\r\n" +

  "The message text goes here"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

This will give you the following data:

RnJvbTogc2VuZGVyQGdtYWlsLmNvbQ0KVG86IHJlY2VpdmVyQGdtYWlsLmNvbQ0KU3ViamVjdDogU3ViamVjdCBUZXh0DQoNClRoZSBtZXNzYWdlIHRleHQgZ29lcyBoZXJl

Use this string above as your raw-parameter in the API Explorer to send the mail.

Tholle
  • 108,070
  • 19
  • 198
  • 189
  • Can anyone share any link or res, on how to decode the above encoded data, back to original message string.? – Rohit Nandi Jan 26 '19 at 12:54
  • @RohitNandi You can try the [`atob`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob) function. – Tholle Jan 26 '19 at 13:10
0

you can use this in node REPL or online node compiler and get the json

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())
Gal Morad
  • 806
  • 7
  • 7