Error 413: Request Entity Too Large with PHP

落爺英雄遲暮 提交于 2020-01-03 02:20:32

问题


I'm using Gmail's PHP API to send emails. Using those resources, I can send messages with attachments upto 5 mb.

But I can't figure out how to send attachments larger than 5 MB. I've found that it is necessary to use multipart uploadtype, but I can not figure out exactly how to implement that based on what I tried is:

$service->users_messages->send($userId, $message, 'uploadType' => 'resumable']);

$service->users_messages->send($userId, $message, 'uploadType' => 'multipart']);

still getting Error 413: Request Entity Too Large

Already researched on internet but not able to make it working.

Edit: Below codes give me Request is too large. even for 5 mb file

$mail->preSend();
$mime = $mail->getSentMIMEMessage();

$raw = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$message = new Google_Service_Gmail_Message();
$message->setRaw($raw);
$message->setThreadId($threadId); //only for reply
$sendOptions = [
     'uploadType' => 'resumable'
];
// size of chunks we are going to send to google
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$response = $service->users_messages->send($userId, $message, $sendOptions);
 // create mediafile upload
  $media = new Google_Http_MediaFileUpload(
          $client,
          $response,
          'message/rfc822',
          $raw,
          true,
          $chunkSizeBytes
   );
   $media->setFileSize(strlen($raw));
   // Upload the various chunks. $status will be false until the process is complete.
   $status = false;

    while (! $status) {
       $status = $media->nextChunk();
       echo $status ."<br>";
    }

     // Reset to the client to execute requests immediately in the future.
      $client->setDefer(false);

      // Get sent email message id
      $googleMessageId = $status->getId();

Here they suggest to Remove $message->setRaw($raw); . If I remove this line than I get Recipient address required error.

How I fixed it:

$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->addAddress($to);
$mail->AddCC($cc);
$mail->AddBCC($bcc);
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$sendOptions = [ 'uploadType' => 'resumable' ]; 
$client->setDefer(true);
$chunkSizeBytes = 1 * 1024 * 1024;
// create mediafile upload
$media = new Google_Http_MediaFileUpload(
          $client,
          $response,
          'message/rfc822',
          $mime,
          true,
          $chunkSizeBytes
      );
  $response = $service->users_messages->send($userId, $message);
  $media->setFileSize(strlen($mime));
      // Upload the various chunks. $status will be false until the process is complete.
      $status = false;
      while (! $status) {
        $status = $media->nextChunk();
      }
      //Reset to the client to execute requests immediately in the future.
      $client->setDefer(false);
      // Get sent email message id
      $googleMessageId = $status->getId();

来源:https://stackoverflow.com/questions/54022660/error-413-request-entity-too-large-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!