【问题标题】:Gmail API PHP Client Library - How do you send large attachments using the PHP client library?Gmail API PHP 客户端库 - 如何使用 PHP 客户端库发送大型附件?
【发布时间】:2015-05-18 20:00:57
【问题描述】:

我正在使用 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,而不是内部服务器错误。

【问题讨论】:

  • 我不是 100% 确定,但我认为 gmail 的一般限制为 4MB。分段上传只是使您能够通过一个请求发送元数据和媒体。 (如果我错了,请告诉我......)
  • 这必须是 google 的限制,否则您可以进入服务器上的 php.ini conf 并将帖子大小限制增加到任何大小。
  • 根据this page,最大上传大小实际上是35 MB。 php.ini 中指定的大小足以满足此要求,请求失败为来自 google 的 413,而不是内部服务器错误。

标签: php gmail email-attachments google-api-php-client gmail-api


【解决方案1】:

如果你已经准备好了原始邮件,可以使用这个例子(原文取自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();

【讨论】:

  • 我不明白您的实际文件在代码中的位置是什么?
  • @LargeTuna 它来自 $mailMessage,它必须是原始电子邮件文本。
  • 天啊!!!你的智慧启发了我。非常感谢。但说真的,非常感谢你!完美的例子。它工作正常。
  • 希望能给你100个赞?
  • 这给了我一个关于请求太大的错误。这发生在 SDK 进行的第一个 API 调用中,以获取上传的可恢复 URL。由于某种原因,它传递的主体对于该请求来说“太大”。就我而言,MIME 消息大约为 12mb。
【解决方案2】:

不太熟悉 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);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多