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();
}