如何使用 gmail API 发送大附件 [英] How to send big attachments with gmail API

查看:22
本文介绍了如何使用 gmail API 发送大附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to send an email with Gmail API. I'm able to send it with smaller files successfully. Problem appears when I try to send attachment with bigger size. It's been couple of hours I'm trying different solutions. Before it was giving error Error 413: Request Entity Too Large. I updated my code and it looks like this:

    $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 错误.请在这方面帮助我.

By the way the file i'm trying to send is 7.x MB and after mime message creation the size of whole mime message becomes around 14MB and API limitation to send message is 35 MB. Why it is giving Request is too large error. Please help me in this regard.

推荐答案

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

In case anyone else is facing same problem. I solved my problem. There was bug in my program. I was passing encoded $mime to media constructor. It should be pass un-encoded mime. Overall changes were following to make it working.

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

成功了!!

这篇关于如何使用 gmail API 发送大附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆