错误413:使用PHP

时间:2019-01-03 12:50:04

标签: php gmail-api google-api-php-client

我正在使用Gmail的PHP API发送电子邮件。使用这些资源,我可以发送附件最大为5 mb的邮件。

但是我不知道如何发送大于5 MB的附件。我发现有必要使用分段上传类型,但根据我尝试的方法,我无法弄清楚如何实现该目标:

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

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

仍然出现错误413:请求实体太大

已经在互联网上进行了研究,但无法使其正常工作。

编辑:以下代码即使是5 mb的文件也能给我Request is too large.

$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,他们建议删除$message->setRaw($raw);。如果删除此行,则会出现Recipient address required错误。

我如何修复它:

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

0 个答案:

没有答案