Gmail API PHP 客户端库 - 如何使用 PHP 客户端库发送大型附件?
Gmail API PHP Client Library - How do you send large attachments using the PHP client library?
我正在使用 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 的附件。我发现有必要使用 multipart uploadtype,但我可以根据我目前的情况确切地弄清楚如何实现它,因为上面的代码仍然给我这个错误:
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,而不是内部服务器错误。
不太熟悉 GMail API,但您应该能够使用分块上传来减少每个单独请求的大小。查看客户端中的文件上传示例 Github: https://github.com/google/google-api-php-client/blob/master/examples/fileupload.php#L73
$media = new Google_Http_MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
$status = false;
$handle = fopen(TESTFILE, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
如果你已经准备好原始邮件,你可以使用这个例子(最初来自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();
我正在使用 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 的附件。我发现有必要使用 multipart uploadtype,但我可以根据我目前的情况确切地弄清楚如何实现它,因为上面的代码仍然给我这个错误:
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,而不是内部服务器错误。
不太熟悉 GMail API,但您应该能够使用分块上传来减少每个单独请求的大小。查看客户端中的文件上传示例 Github: https://github.com/google/google-api-php-client/blob/master/examples/fileupload.php#L73
$media = new Google_Http_MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
$status = false;
$handle = fopen(TESTFILE, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
如果你已经准备好原始邮件,你可以使用这个例子(最初来自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();