Gmail API PHP Client Library - How do you send large attachments using the PHP client library?

那年仲夏 提交于 2019-12-01 12:16:26

Not too familiar with the GMail API, but you should be able to use chunked uploads to reduce the size of each individual request. Take a look at the file upload sample in the client 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);
}

If you have already prepared raw email, you can use this example (originally taken from 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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!