我正在使用 Google 的 PHP 客户端库发送对 Gmail API 的调用。使用这些资源,我可以使用如下代码发送带有附件的消息:
public function send_message(Google_Service_Gmail $gmail, $raw_message)
{
Log::info('Gmail API request \'users_messages->send\'');
$postBody = new Google_Service_Gmail_Message();
$postBody->setRaw(Str::base64_encode_url($raw_message));
return $gmail->users_messages->send('me', $postBody, ['uploadType' => 'multipart']);
}
但我怎么也想不出如何发送大于几 MB 的附件。我发现有必要使用分段上传类型,但我可以根据我目前拥有的内容准确地弄清楚如何实现它,因为上面的代码仍然给我这个错误:
Error calling POST https://www.googleapis.com/gmail/v1/users/me/messages/send?uploadType=multipart
Error 413: Request Entity Too Large
This article具有非常好的大体信息,但我希望获得更多针对 Google 的 PHP 客户端库的指导。
编辑:根据this page ,最大上传大小实际上是 35 MB。我的 php.ini 中指定的大小足以满足此目的,并且请求失败为来自 google 的 413,而不是内部服务器错误。
最佳答案
如果您已经准备好原始电子邮件,则可以使用此示例(最初摘自 here ):
// size of chunks we are going to send to google
$chunkSizeBytes = 1 * 1024 * 1024;
// actual raw email message
$mailMessage = 'raw email text with files embedded'
// code to create mime message
$googleClient = new Google_Client();
// code to setup the client
$mailService = new Google_Service_Gmail($googleClient);
$message = new Google_Service_Gmail_Message();
// Call the API with the media upload, defer so it doesn't immediately return.
$googleClient->setDefer(true);
$request = $mailService->users_messages->send('me', $message);
// create mediafile upload
$media = new Google_Http_MediaFileUpload(
$googleClient,
$request,
'message/rfc822',
$mailMessage,
true,
$chunkSizeBytes
);
$media->setFileSize(strlen($mailMessage));
// Upload the various chunks. $status will be false until the process is complete.
$status = false;
while (! $status) {
$status = $media->nextChunk();
}
// Reset to the client to execute requests immediately in the future.
$googleClient->setDefer(false);
// Get sent email message id
$googleMessageId = $status->getId();
关于php - Gmail API PHP 客户端库 - 如何使用 PHP 客户端库发送大附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29081343/