如何使用gmail API发送大附件

时间:2018-06-11 20:29:34

标签: php gmail gmail-api

我正在尝试使用Gmail API发送电子邮件。我能够成功地使用较小的文件发送它。当我尝试发送更大尺寸的附件时出现问题。我花了几个小时尝试不同的解决方案。在它出错之前错误413:请求实体太大。我更新了我的代码,它看起来像这样:

    $mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
$sendOptions = [
        'uploadType' => 'resumable'
];
// size of chunks we are going to send to google
$chunkSizeBytes = 1 * 1024 * 1024;

$client->setDefer(true);
$objSentMsg = $service->users_messages->send('me', $msg, $sendOptions);

// create mediafile upload
$media = new Google_Http_MediaFileUpload(
    $client,
    $objSentMsg,
    'message/rfc822',
    $mime,
    true,
    $chunkSizeBytes
);
//I tried to pass null in above media object in place of $mime variable. didn't worked either.


$media->setFileSize(strlen($mime));

// 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 = $result->getId();

现在它给了我错误:未捕获Google_Service_Exception:请求太大

顺便说一下,我试图发送的文件是7.x MB,并且在创建mime消息之后,整个mime消息的大小变为大约14MB,发送消息的API限制为35 MB。为什么要提供请求太大错误。请帮助我。

2 个答案:

答案 0 :(得分:1)

gmail有文件大小限制尝试上传到Google云端硬盘并分享

答案 1 :(得分:1)

如果其他人遇到同样的问题。我解决了我的问题。我的程序中有错误。我正在将编码的$ mime传递给媒体构造函数。它应该是通过未编码的mime。随后的总体变化使其发挥作用。

  1. 已删除 $ msg-&gt; setRaw($ mime); 此行。
  2. 将未解码的$ mime传递给媒体构造函数。
  3. 将未解码的mime大小传递给此行 $ media-&gt; setFileSize(strlen($ mime));
  4. 它有效!!

相关问题