How to send big attachments with gmail API(如何使用 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.
- 删除了 $msg->setRaw($mime); 这一行.
- 将未解码的 $mime 传递给媒体构造函数.
- 将未解码的 mime 大小传递到此行 $media->setFileSize(strlen($mime));
成功了!!
这篇关于如何使用 gmail API 发送大附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 gmail API 发送大附件


- 从 PHP 中的输入表单获取日期 2022-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01