2

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);
Yehuda
  • 157
  • 12
  • The docs says use [resumable upload](https://developers.google.com/gmail/api/guides/uploads#resumable) for larger files. Did you try that? – ReyAnthonyRenacia Mar 20 '18 at 11:10
  • Did you read my code ? – Yehuda Mar 20 '18 at 19:31
  • Possible duplicate of [How to send big attachments with gmail API](https://stackoverflow.com/questions/50805550/how-to-send-big-attachments-with-gmail-api) – Jake Symons Jun 12 '18 at 10:52
  • @JakeSymons, Really? how can I be a duplicate of a question asked yesterday? – Yehuda Jun 13 '18 at 11:20
  • @YehudaHassine, were you able to figure out the issue? I am also facing the same problem. If I attach one file of 3.7MB payload size is 7.5MB and if I upload two such files it goes to 14MB and I am getting "Entity too large" error. If you were able to fix, please share. – M P May 12 '19 at 03:02

1 Answers1

0

@MP In my case, the problem is that I needed to set the unencoded message size, not the actual file. like this:

$media->setFileSize(strlen($message));
Yehuda
  • 157
  • 12