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 SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01