php - Gmail API PHP 客户端库 - 如何使用 PHP 客户端库发送大附件?

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

我正在使用 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/

相关文章:

python - 如何只从特定的 gmail 标签下载未读的附件?

powershell - 如何在 PowerShell 代码中发送带有附件的电子邮件?

php - Codeigniter Assets 文件夹最佳实践

php - 在 IE 中 : my CSS has been changed while using a mysql based SESSION with php

php - 尽可能低的预期 HTTPS 延迟?

java - 如何使用javamail正确删除复制的gmail消息?

ios - 检查是否安装了Facebook,LinkedIn,Gmail应用程序

php - 有没有办法让 saveAll() 删除无关的对象?

Android - 在电子邮件 Intent 中附加多个文件会使 Gmail 崩溃

android - Gmail "Can' t 附加空文件”