0

I have reviewed some similar questions on stackoverflow, but none of the answers seem to be working for me. Especially I have tried the options ['uploadType' => 'multipart'] and ['uploadType' => 'resumable'] when sending email. It always gives "Request Entity Too Large Error 413" if the attachment is over 5MB (I tried sending a docx file of 7.x MB and failed). For attachment smaller than 5MB it's OK. Could anyone please help? Many thanks!

Here is my code:

        $objGMail = new Google_Service_Gmail($client);
        $strSubject = "Test message";

        $strRawMessage = "From: <mygmailid@gmail.com>\r\n";
        $strRawMessage .= "To: <receiveremailid@gmail.com>\r\n";
        $strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
        $strRawMessage .= "MIME-Version: 1.0\r\n"; 

        $boundary = uniqid(rand(), true);
        $strRawMessage .= 'Content-type: multipart/related; boundary="' . $boundary . '"' . "\r\n";

        $filePath = "data/bigdocfile.docx";
        $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
        $mimeType = finfo_file($finfo, $filePath);
        $fileName = "bigdocfile.docx";
        $fileData = base64_encode(file_get_contents($filePath));

        $strRawMessage .= "\r\n--{$boundary}\r\n";
        $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n";            
        $strRawMessage .= 'Content-ID: <mygmailid@gmail.com' . '>' . "\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 .= "Content-Type: text/html; charset=utf-8\r\n";
        $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
        $strRawMessage .= $order->content;

        try 
        {                
            $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
            $msg = new Google_Service_Gmail_Message();
            $msg->setRaw($mime);
            $objSentMsg = $objGMail->users_messages->send("me", $msg, ['uploadType' => 'multipart']);
            return "success";
            } catch (Exception $e) {
                unset($_SESSION['gmail_access_token']);
                return $e->getMessage();                
         }
Tao Hu
  • 57
  • 1
  • 9
  • 3
    Possible duplicate of [Gmail API PHP - Request Entity Too Large Error 413](https://stackoverflow.com/questions/47679102/gmail-api-php-request-entity-too-large-error-413) – Linda Lawton - DaImTo Sep 13 '19 at 11:59
  • it is. But the answers to that question does not help at all. I clearly said I had tried the "multipart" and "resumable" options. – Tao Hu Sep 14 '19 at 01:15
  • for attachments between 5 mb and 50 mb you need to use multi part. This question is the same as the other one. There is no reason to have two questions on SO that are the same. If you are having issues with the multi part you should ask on the original question for clarification on the answer. – Linda Lawton - DaImTo Sep 14 '19 at 11:13
  • @DaImTo First, are you sure multipart is going to solve the problem? In the "possible duplicate" question, there are no answers accepted by the question owner. Second, if multipart is really the solution, it means the issue comes from elsewhere in my code. So it makes sense for me to put up my code here and let people see it. – Tao Hu Sep 17 '19 at 03:08

0 Answers0