title: Getting Request Entity Too Large error 413 while sending attachment over 5 MB size using Gmail API in php
body: <p>Error :</p>
  
  <blockquote>
    <p>Fatal error: Uncaught Google_Service_Exception:  
    Request Entity Too Large   <H1>Request Entity Too Large</H1>
    <H2>Error 413</H2>   in
    C:\xampp\htdocs\gmail\vendor\google\apiclient\src\Google\Http\REST.php:118
    Stack trace: #0
    C:\xampp\htdocs\gmail\vendor\google\apiclient\src\Google\Http\REST.php(94):
    Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response),
    Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1
    C:\xampp\htdocs\gmail\vendor\google\apiclient\src\Google\Task\Runner.php(176):
    Google_Http_REST::doExecute(Object(GuzzleHttp\Client),
    Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2
    C:\xampp\htdocs\gmail\vendor\google\apiclient\src\Google\Http\REST.php(58):
    Google_Task_Runner->run() #3
    C:\xampp\htdocs\gmail\vendor\google\apiclient\src\Google\Client.php(798):
    Google_Http_REST::execute(Object(GuzzleHttp\Client),
    Object(GuzzleHttp\Psr7\Request), 'Google_Service_...', Array) #4
    C:\xampp\htdocs\gmail\vendor\ in
    C:\xampp\htdocs\gmail\vendor\google\apiclient\src\Google\Http\REST.php
    on line 118</p>
    
    <p>Code :$strRawMessage .= 'To: ' . encodeRecipients(" &lt;" . $strToMail .
    ">") . "\r\n";    $strRawMessage .= 'Subject: =?utf-8?B?' .
    base64_encode($strSubject) . "?=\r\n";    $strRawMessage .=
    "MIME-Version: 1.0\r\n";  $strRawMessage .= 'Content-type: resumable;
    boundary="' . $boundary . '"' . "\r\n";   $strRawMessage .=
    'Content-Transfer-Encoding: chunked' . "\r\n\r\n";</p>
    
    <p>/*************Attachment**************/     $basename =
    basename($filePath);  $fileTempPath = sys_get_temp_dir() . "/" .
    $basename;    file_put_contents($fileTempPath,
    file_get_contents($filePath));    $finfo =
    finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype
    extension     $mimeType = finfo_file($finfo, $fileTempPath);  $fileData =
    base64_encode(file_get_contents($fileTempPath));  $strRawMessage .=
    "\r\n--{$boundary}\r\n";  $strRawMessage .= 'Content-Type: ' .
    $mimeType . '; name="' . $basename . '";' . "\r\n";   //$strRawMessage
    .= 'Content-Type: message/rfc822 ' . "\r\n";  $strRawMessage .=
    'Content-ID: &lt;' . $basename . '>' . "\r\n";   $strRawMessage .=
    'Content-Length: ' . filesize($fileTempPath) . "\r\n";    $strRawMessage
    .= 'X-Upload-Content-Type: message/rfc822' . "\r\n";  $strRawMessage
    .= 'X-Upload-Content-Length: ' . filesize($fileTempPath) . "\r\n";
      $strRawMessage .= 'Content-Description: ' . $basename . ';' . "\r\n";
      $strRawMessage .= 'Content-Disposition: attachment; filename="' .
    $basename . '"; size=' . filesize($fileTempPath) . ';' . "\r\n";
      $strRawMessage .= 'Content-Transfer-Encoding: chunked' . "\r\n\r\n";
      $strRawMessage .= chunk_split($fileData, 76, "\r\n") . "\r\n";
      $strRawMessage .= "--{$boundary}\r\n";</p>
  </blockquote>
  
  <pre><code>/*************Attachment**************/
  
  $strRawMessage .= "--{$boundary}\r\n";
  $strRawMessage .= 'Content-Type: application/json; charset=' . $charset . "\r\n";
  //$strRawMessage .= 'Content-Transfer-Encoding: 8bit' . "\r\n\r\n";
  $strRawMessage .= $strMessage . "\r\n";
  $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
  $message-&gt;setRaw($mime);
  $messagesResponse = $service-&gt;users_messages-&gt;send('me', $message, ['uploadType' =&gt; 'resumable ']);
  </code></pre>