2

I want to send messages with attachments size of 5MB over (under 25MB). I'm using the google-api-php-client with Mail_mime.

I wrote this code and got the error message "Request is too large".

 $mime = new Mail_mime();

 $mime->setFrom($from_enc);
 $mime->addTo($to_enc);
 $mime->addCc($cc_enc);
 $mime->addBcc($bcc_enc);

 $mime->setSubject(mb_encode_mimeheader($subject, "ISO-2022-JP"));

 $body = $_POST['body'];
 $mime->setTXTBody(convertEOL($body));
 $mime->setHTMLBody(convertEOL($body, "<br/>"));

 $mime->addAttachment($attachment
 ,'application/octet-stream'  // content-type
 ,mb_convert_encoding("test.zip", "ISO-2022-JP") // attached file name
 ,true // isfile
 ,'base64' // encoding
 ,'attachment' // disposition attachment
 ,'ISO-2022-JP' // charset
 ,'' // language
 ,'' // location
 ,'base64' // n_encoding
 ,'base64' // f_encoding Encoding of the attachment's filename in Content-Disposition header.
 ,'' // description
 ,'ISO-2022-JP' // h_charset
 );

 $message_body = $mime->getMessage();


 $msg = base64url_encode($message_body);

 $message = new Google_Service_Gmail_Message();
 $message->setRaw($msg);

 $client->setDefer(true);
 $request = $service->users_messages->send('me', $message);

 $chunkSizeBytes = 1 * 1024 * 1024;
 $media = new Google_Http_MediaFileUpload(
    $client,
    $request,
    'message/rfc822',
    null,
    true,
    $chunkSizeBytes
 );
 $media->setFileSize(filesize($attachment));

 $status = false;
 $handle = fopen($attachments[0], "rb");
 while (!$status && !feof($handle)) {
     $chunk = fread($handle, $chunkSizeBytes);
     $status = $media->nextChunk();
 }
 $result = false;
 if($status != false) {
     $result = $status;
 }
 fclose($handle);
 $client-setDefer(false);


function convertEOL($string, $to = "\n")
{
    return strtr($string, array_fill_keys(array("\r\n", "\r", "\n"), $to));
}

function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
 }

And trace dumps.

string(70) "https://www.googleapis.com/gmail/v1/users/me/messages/15be362e72b198c4"
string(509) "https://www.googleapis.com/gmail/v1/users/me/messages/15be362e72b198c4/atta…aqwWpMiLfhPU85gCGTR2Aa9NTIXuSnd6l8cQDI8sNoaAIBixPFpqLmAnJG1BYChXKNNljSJ_uw"
string(70) "https://www.googleapis.com/gmail/v1/users/me/messages/15be362e72b198c4"
array(5) {
  ["content-type"]=>
  string(31) "application/json; charset=UTF-8"
  ["content-length"]=>
  int(10439601)
  ["x-upload-content-type"]=>
  string(14) "message/rfc822"
  ["x-upload-content-length"]=>
  int(5711788)
  ["expect"]=>
  string(0) ""
}
string(86) "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=resumable"
Request is too large.

Next, I tried do not call setRaw method and set parameter of body message to Google_Http_MediaFileUpload's constractor.

$media = new Google_Http_MediaFileUpload(
  $client,
  $request,
  'message/rfc822',
  $msg,  // set encoded message body
  true,
  $chunkSizeBytes
);

Then I got a message "Failed to parse Content-Range header".

I don't know why got this errors.

Thanks.

hata
  • 21
  • 1
  • This may help: https://stackoverflow.com/questions/24462184/413-request-entity-too-large – Blue May 08 '17 at 02:51
  • Possible duplicate of [413 - Request Entity Too Large](http://stackoverflow.com/questions/24462184/413-request-entity-too-large) – Linda Lawton - DaImTo May 08 '17 at 07:18
  • @FrankerZ,DalmTo Thanks your help. I guess that my mime message has problem. – hata May 12 '17 at 02:38
  • I solved this problem. It is my program bug. I thought set encoded message body to Google_Http_MediaFileUpload's constructor, but never set encoded message body. It must be no-encoded mime. – hata Jun 07 '17 at 07:04
  • @hata thanks a lot. your last comment helped me a lot to solve my same problem :) I was passing encoded msg too. Now it is working fine ;) – Asad ullah Jun 12 '18 at 10:36
  • @Asadullah Thank you for your comment. I'm glad to hear that :) – hata Jun 21 '18 at 02:15

0 Answers0