Search code examples
apirestgmail-api

Google REST API - message in an RFC 2822 formatted and base64url encoded string


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 ?


Solution

  • 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.