title: Send email via Gmail API (Javascript)
body: <p>I'm trying to get a hello world sending a mail via the JS Gmail API.  I have authorised correctly (labels can be listed) according to <a href="https://developers.google.com/gmail/api/quickstart/js" rel="nofollow noreferrer">this</a>.</p>
  
  <p>I'm using the following code, running in the browser:</p>
  
  <pre class="lang-js prettyprint-override"><code>const message =
  "From: my.email@gmail.com\r\n" + 
  "To: my.email@gmail.com\r\n" +
  "Subject: As basic as it gets\r\n\r\n" +
  "This is the plain text body of the message.  Note the blank line between the header information and the body of the message.";
  
  
  // The body needs to be base64url encoded.
  const encodedMessage = btoa(message)
  
  const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
  
  gapi.client.gmail.users.messages.send({
      userId: 'me',
      requestBody: {
          // same response with any of these
          raw: reallyEncodedMessage
          // raw: encodedMessage
          // raw: message
      }
  }).then(function () { console.log("done!")});
  </code></pre>
  
  <p>This gives an HTTP 400 response:</p>
  
  <pre><code>{
   "error": {
    "errors": [
     {
      "domain": "global",
      "reason": "invalidArgument",
      "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
     }
    ],
    "code": 400,
    "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
   }
  }
  </code></pre>
  
  <p>This is using JS API available at <code>https://apis.google.com/js/api.js</code>.  RFC822 example taken from <a href="https://msdn.microsoft.com/en-us/library/ms527253(v=exchg.10).aspx" rel="nofollow noreferrer">MSDN</a> and <a href="https://stackoverflow.com/a/29133630/614523">elsewhere</a>.  Web-safe base64 encoding the RFC822 message as far as I can tell is a the standard with this API.  Same error in both Firefox and Chrome.</p>
  
  <p>Where am I going wrong?</p>