According to Gmail API to send an email with large attachments bigger then 5MB you need to use the instructions here: https://developers.google.com/gmail/api/guides/uploads
The API is a not very clear about the details and I tried to use the explanation I found here: Gmail API PHP Client Library - How do you send large attachments using the PHP client library?
I get "Entity too large" error message every time.
Someone can succeded to send an attachment bigger than 5MB with Gmail API, and help me understand what I'm doing wrong?
My composer file:
{
"require": {
"google/apiclient": "^2.0",
"pear/mail_mime": "^1.10"
}
}
My code after reading everything is this:
<?php
set_time_limit(100);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/vendor/autoload.php';
require_once 'client.php';
$googleClient = getClient();
$mailService = new Google_Service_Gmail($googleClient);
$message = new Google_Service_Gmail_Message;
$file = __DIR__ . '/pexels-photo.jpg';
$mime = new Mail_mime;
$mime->addTo('mymail@domain.com');
$mime->setTXTBody('');
$mime->setSubject('test');
$mailMessage = base64_encode($mime->getMessage());
$message->setRaw($mailMessage);
$request = $mailService->users_messages->send(
'me',
$message,
array( 'uploadType' => 'resumable' )
);
$googleClient->setDefer(true);
$media = new Google_Http_MediaFileUpload(
$googleClient,
$request,
'message/rfc822',
$mailMessage,
$resumable = true,
$chunkSizeBytes = 1 * 1024 * 1024
);
$media->setFileSize(filesize($file));
$status = false;
$handle = fopen($file, 'rb');
while (! $status && ! feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$googleClient->setDefer(false);