title: Gmail API: 400 bad request when trying to send an email (PHP code)
body: <p>I expected the code below to send an email, but I'm only getting this:</p>
  
  <blockquote>
    <p>An error occurred: Error calling POST
    <a href="https://www.googleapis.com/gmail/v1/users/me/messages/send">https://www.googleapis.com/gmail/v1/users/me/messages/send</a>: (400) Bad
    Request</p>
  </blockquote>
  
  <p>I get a <code>200 OK</code> using the Google Developers Console <a href="https://developers.google.com/gmail/api/v1/reference/users/messages/send?hl=es">here</a> at the bottom. Any help?</p>
  
  <pre><code>$client_id = '599901532082-js1r50n20q6n5mir9fo1g81qkj9kfn3j.apps.googleusercontent.com';
  $service_account_name = '599901532082-js1r50n20q6n5mir9fo1g81qkj9kfn3j@developer.gserviceaccount.com';
  $key_file_location = '/tmp/APIProject-cb6558ba6435.p12';
  
  $client = new \Google_Client();
  $client-&gt;setApplicationName("Client_Library_Examples");
  $service = new \Google_Service_Gmail($client);  
  
  if (isset($_SESSION['service_token'])) {
    $client-&gt;setAccessToken($_SESSION['service_token']);
  }
  $key = file_get_contents($key_file_location);
  $cred = new \Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.compose'),
    $key
  );
  $client-&gt;setAssertionCredentials($cred);
  
  if ($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {
    $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred);
  }
  //check if you want the validity of this string at: http://www.komeil.com/toolbox/base64decoder
  //it is web safe base64 encoded email
  $mime = "RnJvbTogSm9obiBEb2UgPHRpcmVuZ2FyZmlvQGdtYWlsLmVzPiANClRvOiBNYXJ5IFNtaXRoIDx0aXJlbmdhcmZpb0BnbWFpbC5jb20-IA0KU3ViamVjdDogU2F5aW5nIEhlbGxvIA0KRGF0ZTogRnJpLCAyMSBOb3YgMTk5NyAwOTo1NTowNiAtMDYwMCANCk1lc3NhZ2UtSUQ6IDwxMjM0QGxvY2FsLm1hY2hpbmUuZXhhbXBsZT4NCg0KVGhpcyBpcyBhIG1lc3NhZ2UganVzdCB0byBzYXkgaGVsbG8uIFNvLCAiSGVsbG8iLg==";
  
  
  $service = new \Google_Service_Gmail($client);
  
  $msg = new \Google_Service_Gmail_Message();
  $msg-&gt;setRaw($mime);
  
  try {
    $results = $service-&gt;users_messages-&gt;send("me", $msg);
    print 'Message with ID: ' . $message-&gt;getId() . ' sent.';
    return $message;
  } catch (\Exception $e) {
    print 'An error occurred: ' . $e-&gt;getMessage();
  
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>this is the request object. It includes the response data also:</p>
  
  <pre><code>object(Google_Http_Request)[508]
    private 'batchHeaders' =&gt; 
      array (size=3)
        'Content-Type' =&gt; string 'application/http' (length=16)
        'Content-Transfer-Encoding' =&gt; string 'binary' (length=6)
        'MIME-Version' =&gt; string '1.0' (length=3)
    protected 'queryParams' =&gt; 
      array (size=0)
        empty
    protected 'requestMethod' =&gt; string 'POST' (length=4)
    protected 'requestHeaders' =&gt; 
      array (size=3)
        'content-type' =&gt; string 'application/json; charset=UTF-8' (length=31)
        'authorization' =&gt; string 'Bearer ya29.8gEUMiBLfxS8OLdSmpiQ-EcumeATo2qFAfPtPqwTw9fQ2zVrfZaA1X5OLoBmQccrXr8V8g' (length=82)
        'accept-encoding' =&gt; string 'gzip' (length=4)
    protected 'baseComponent' =&gt; string 'https://www.googleapis.com' (length=26)
    protected 'path' =&gt; string '/gmail/v1/users/me/messages/send' (length=32)
    protected 'postBody' =&gt; string '{"raw":"RnJvbTogSm9obiBEb2UgPHRpcmVuZ2FyZmlvQGdtYWlsLmVzPg0KVG86IE1hcnkgU21pdGggPHRpcmVuZ2FyZmlvQGdtYWlsLmNvbT4NClN1YmplY3Q6IFNheWluZyBIZWxsbw0KDQpUaGlzIGlzIGEgbWVzc2FnZSBqdXN0IHRvIHNheSBoZWxsby4gU28sICdIZWxsbycu"}' (length=214)
    protected 'userAgent' =&gt; string 'Client_Library_Examples google-api-php-client/1.0.6-beta (gzip)' (length=63)
    protected 'canGzip' =&gt; boolean true
    protected 'responseHttpCode' =&gt; null
    protected 'responseHeaders' =&gt; null
    protected 'responseBody' =&gt; null
    protected 'expectedClass' =&gt; string 'Google_Service_Gmail_Message' (length=28)
    public 'accessKey' =&gt; null
  
  object(Google_Http_Request)[508]
    private 'batchHeaders' =&gt; 
      array (size=3)
        'Content-Type' =&gt; string 'application/http' (length=16)
        'Content-Transfer-Encoding' =&gt; string 'binary' (length=6)
        'MIME-Version' =&gt; string '1.0' (length=3)
    protected 'queryParams' =&gt; 
      array (size=0)
        empty
    protected 'requestMethod' =&gt; string 'POST' (length=4)
    protected 'requestHeaders' =&gt; 
      array (size=4)
        'content-type' =&gt; string 'application/json; charset=UTF-8' (length=31)
        'authorization' =&gt; string 'Bearer ya29.8gEUM***fxS8OLdSmpiQ-EcumeATo2qFAfPtPqwTw9fQ2zVrfZaA1X5OLoBmQccrXr8V8g' (length=82)
        'accept-encoding' =&gt; string 'gzip' (length=4)
        'content-length' =&gt; int 214
    protected 'baseComponent' =&gt; string 'https://www.googleapis.com' (length=26)
    protected 'path' =&gt; string '/gmail/v1/users/me/messages/send' (length=32)
    protected 'postBody' =&gt; string '{"raw":"RnJvbTogSm9obiBEb2UgPHRpcmVuZ2FyZmlvQGdtYWlsLmVzPg0KVG86IE1hcnkgU21pdGggPHRpcmVuZ2FyZmlvQGdtYWlsLmNvbT4NClN1YmplY3Q6IFNheWluZyBIZWxsbw0KDQpUaGlzIGlzIGEgbWVzc2FnZSBqdXN0IHRvIHNheSBoZWxsby4gU28sICdIZWxsbycu"}' (length=214)
    protected 'userAgent' =&gt; string 'Client_Library_Examples google-api-php-client/1.0.6-beta (gzip)' (length=63)
    protected 'canGzip' =&gt; boolean true
    protected 'responseHttpCode' =&gt; int 400
    protected 'responseHeaders' =&gt; 
      array (size=13)
        'vary' =&gt; string 'Origin
  X-Origin' (length=15)
        'content-type' =&gt; string 'application/json; charset=UTF-8' (length=31)
        'content-encoding' =&gt; string 'gzip' (length=4)
        'date' =&gt; string 'Fri, 18 Sep 2015 08:34:25 GMT' (length=29)
        'expires' =&gt; string 'Fri, 18 Sep 2015 08:34:25 GMT' (length=29)
        'cache-control' =&gt; string 'private, max-age=0' (length=18)
        'x-content-type-options' =&gt; string 'nosniff' (length=7)
        'x-frame-options' =&gt; string 'SAMEORIGIN' (length=10)
        'x-xss-protection' =&gt; string '1; mode=block' (length=13)
        'server' =&gt; string 'GSE' (length=3)
        'alternate-protocol' =&gt; string '443:quic,p=1' (length=12)
        'alt-svc' =&gt; string 'quic=":443"; p="1"; ma=604800' (length=29)
        'transfer-encoding' =&gt; string 'chunked' (length=7)
    protected 'responseBody' =&gt; string '{
   "error": {
    "errors": [
     {
      "domain": "global",
      "reason": "failedPrecondition",
      "message": "Bad Request"
     }
    ],
    "code": 400,
    "message": "Bad Request"
   }
  }
  ' (length=179)
    protected 'expectedClass' =&gt; string 'Google_Service_Gmail_Message' (length=28)
    public 'accessKey' =&gt; null
  </code></pre>