【问题标题】:How to send big attachments with gmail API如何使用 gmail API 发送大附件
【发布时间】:2018-11-21 03:58:33
【问题描述】:

我正在尝试使用 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();

现在它给了我错误:Uncaught Google_Service_Exception: Request is too large

顺便说一下,我尝试发送的文件是 7.x MB,在创建 mime 消息后,整个 mime 消息的大小变为 14MB 左右,发送消息的 API 限制为 35 MB。为什么它给出 Request is too large 错误。请在这方面帮助我。

【问题讨论】:

    标签: php gmail gmail-api


    【解决方案1】:

    以防其他人面临同样的问题。我解决了我的问题。我的程序有错误。我将编码的 $mime 传递给媒体构造函数。它应该通过未编码的 mime。为了使其正常工作,我们进行了整体更改。

    1. 删除了 $msg->setRaw($mime); 这一行。
    2. 将未解码的 $mime 传递给媒体构造函数。
    3. 将未解码的 mime 大小传递到此行 $media->setFileSize(strlen($mime));

    成功了!!

    【讨论】:

    • 我无法让它工作。你能在这里帮忙link 吗?
    【解决方案2】:

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

    【讨论】:

    • 只要有通过附件发送文件的选项小于 35MB。我必须通过附件发送。所以需要那个解决方案。
    • 也不要添加无法帮助他人的答案。请改用 cmets 部分。
    猜你喜欢
    • 2018-08-27
    • 2019-01-13
    • 2019-09-02
    • 2015-03-08
    • 2018-11-11
    • 2019-03-08
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多