title: send email via google http rest api
body: <p>I am trying to send email from NodeJS running on a Linux server to Google Gmail RESR HTTP API. Not using libraries, just sending <code>https</code>. I have figured out the OAuth part, have an access token and get responses from google.  But I cannot get past various error messages.  I have posted the code below. It is not obvious but <code>EmailSend()</code> is called after I get the access token from google, so yeah it is being called.</p>
  <pre><code>var emailStr = new Buffer(
        &quot;Content-Type: text/plain; charset=\&quot;UTF-8\&quot;\n&quot; +
        &quot;MIME-Version: 1.0\n&quot; +
        &quot;Content-Transfer-Encoding: 7bit\n&quot; +
        &quot;to: SOMEONE@gmail.com\n&quot; +
        &quot;from: SOMEONEELSE@MYDOMAIN.com\n&quot; +
        &quot;subject: Subject Text\n\n&quot; +
  
        &quot;The actual message text goes here&quot;
  ).toString(&quot;base64&quot;).replace(/\+/g, '-').replace(/\//g, '_');
  //var emailBase64UrlSafe = Rtrim( emailStr, '=' );
  //var emailBase64UrlSafe = JsStrToUrlSafe ( emailStr );
  var emailBase64UrlSafe = emailStr;
  
  var http = require('https');
  function EmailSend() {
    
    var post_data = emailBase64UrlSafe;
    var post_options = {
        hostname: 'www.googleapis.com',
        port: '443',
        path: '/gmail/v1/users/me/messages/send',
        method: 'POST',
        headers: {
          &quot;Authorization&quot;: 'Bearer '+googleAccessKey['access_token'],
          &quot;Content-Type&quot; : &quot;application/json; charset=UTF-8&quot;
        },
    };
    console.log( post_options );
  
    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('Response: ' + chunk);
        });
    });
    post_req.write(JSON.stringify({ &quot;raw&quot;: emailBase64UrlSafe }));
    post_req.end();
  }; /* end EmailSend() */
  </code></pre>
  <blockquote>
  <pre><code>Response: {
  </code></pre>
  </blockquote>
  <pre><code>&quot;error&quot;: {
   &quot;errors&quot;: [
    {
     &quot;domain&quot;: &quot;global&quot;,
     &quot;reason&quot;: &quot;failedPrecondition&quot;,
     &quot;message&quot;: &quot;Bad Request&quot;
    }
   ],
   &quot;code&quot;: 400,
   &quot;message&quot;: &quot;Bad Request&quot;
  }
  </code></pre>
  <p>Resources used:</p>
  <ol>
  <li><p><a href="https://www.rfc-editor.org/rfc/rfc2822#appendix-A" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc2822#appendix-A</a></p>
  </li>
  <li><p><a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount" rel="nofollow noreferrer">https://developers.google.com/identity/protocols/OAuth2ServiceAccount</a></p>
  </li>
  <li><p><a href="https://nodejs.org/api/https.html#https_https_request_options_callback" rel="nofollow noreferrer">https://nodejs.org/api/https.html#https_https_request_options_callback</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/29504289/send-email-using-google-api-with-only-access-token?rq=1">Send email using Google API with only access token</a></p>
  </li>
  </ol>