title: Send Mail with attachment using Gmail new API, but it is not displayed for receiver's inbox
body: <p>We are integrating GMail using the latest API provided by them as a part of developing email client within our application. We are using PHP client library provided by Google. </p>
  
  <p>See the link <a href="https://developers.google.com/api-client-library/php/" rel="noreferrer">https://developers.google.com/api-client-library/php/</a></p>
  
  <p>In this I'm trying to send a mail with attachment. Here we have generated message/rfc822 compatible text and passed it with 'raw' parameter. Here the problem I found is, after executing the code, when I checked the sent mail for the mail which I sent via GMail API, the attachments are shown correctly. But it is not received/ displayed for the sender's mail box.</p>
  
  <p>See the code for more info:</p>
  
  <pre><code>require_once DOCUMENT_ROOT . '/../library/google/src/Google/Client.php';
  require_once DOCUMENT_ROOT . '/../library/google/src/Google/Service/Gmail.php';
  
  function encodeRecipients($recipient){
      $recipientsCharset = 'utf-8';
      if (preg_match("/(.*)&lt;(.*)&gt;/", $recipient, $regs)) {
          $recipient = '=?' . $recipientsCharset . '?B?'.base64_encode($regs[1]).'?= &lt;'.$regs[2].'&gt;';
      }
      return $recipient;
  }
  
  $isAccessCodeExpired = 0;
  $arrAccessToken = array();
  $session = new Zend_Session_Namespace();
  
  $client = new Google_Client();
  $client-&gt;setClientId($this-&gt;client_id);
  $client-&gt;setClientSecret($this-&gt;client_secret);
  $client-&gt;setRedirectUri($this-&gt;redirect_uri);
  $client-&gt;setAccessType('offline');
  $client-&gt;setApprovalPrompt('force');
  
  $client-&gt;addScope("https://mail.google.com/");
  $client-&gt;addScope("https://www.googleapis.com/auth/gmail.compose");
  $client-&gt;addScope("https://www.googleapis.com/auth/gmail.modify");
  $client-&gt;addScope("https://www.googleapis.com/auth/gmail.readonly");
  
  
  if ($this-&gt;getRequest()-&gt;getParam('code')) {
      $code = $this-&gt;getRequest()-&gt;getParam('code');
      $client-&gt;authenticate($code);
      $session-&gt;gmail_access_token = $client-&gt;getAccessToken();
      //$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
      $redirect = BASE_PATH . '/oauth2callback';
      header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  
  $isAccessCodeExpired = $client-&gt;isAccessTokenExpired();
  if (isset($session-&gt;gmail_access_token) &amp;&amp; $session-&gt;gmail_access_token != "" &amp;&amp; $isAccessCodeExpired !== 1) {
  
      $client-&gt;setAccessToken($session-&gt;gmail_access_token);            
      $objGMail = new Google_Service_Gmail($client);
  
      $strMailContent = 'This is a test mail which is sent via using Gmail API client library.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;Thanks,&lt;br/&gt;GMail API Team.';
      $strMailTextVersion = strip_tags($strMailContent, '');
  
      $strRawMessage = "";
      $boundary = uniqid(rand(), true);
      $subjectCharset = $charset = 'utf-8';
      $strToMailName = 'To User Name';
      $strToMail = 'toemail@gmail.com';
      $strSesFromName = 'From User Name';
      $strSesFromEmail = 'fromemail@gmail.com';
      $strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');
  
      $strRawMessage .= 'To: ' . encodeRecipients($strToMailName . " &lt;" . $strToMail . "&gt;") . "\r\n";
      $strRawMessage .= 'From: '. encodeRecipients($strSesFromName . " &lt;" . $strSesFromEmail . "&gt;") . "\r\n";
  
      $strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
      $strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
      $strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\r\n";
  
      $filePath = '/home/server/Downloads/credentials.csv';
      $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
      $mimeType = finfo_file($finfo, $filePath);
      $fileName = 'credentials.csv';
      $fileData = base64_encode(file_get_contents($filePath));
  
      $strRawMessage .= "\r\n--{$boundary}\r\n";
      $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n";            
      $strRawMessage .= 'Content-ID: &lt;' . $strSesFromEmail . '&gt;' . "\r\n";            
      $strRawMessage .= 'Content-Description: ' . $fileName . ';' . "\r\n";
      $strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "\r\n";
      $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
      $strRawMessage .= chunk_split(base64_encode(file_get_contents($filePath)), 76, "\n") . "\r\n";
      $strRawMessage .= '--' . $boundary . "\r\n";
  
      $strRawMessage .= "\r\n--{$boundary}\r\n";
      $strRawMessage .= 'Content-Type: text/plain; charset=' . $charset . "\r\n";
      $strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
      $strRawMessage .= $strMailTextVersion . "\r\n";
  
      $strRawMessage .= "--{$boundary}\r\n";
      $strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
      $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
      $strRawMessage .= $strMailContent . "\r\n";
  
      //Send Mails
      //Prepare the message in message/rfc822
      try {
          // The message needs to be encoded in Base64URL
          $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
          $msg = new Google_Service_Gmail_Message();
          $msg-&gt;setRaw($mime);
          $objSentMsg = $objGMail-&gt;users_messages-&gt;send("me", $msg);
  
          print('Message sent object');
          print($objSentMsg);
  
      } catch (Exception $e) {
          print($e-&gt;getMessage());
          unset($_SESSION['access_token']);
      }
  }
  </code></pre>
  
  <p>Please help me...
  Thanks in advance.</p>