1

I'm using Gmail's PHP API to send emails. Using those resources, I can send messages with attachments upto 5 mb.

But I can't figure out how to send attachments larger than 5 MB. I've found that it is necessary to use multipart uploadtype, but I can not figure out exactly how to implement that based on what I tried is:

$service->users_messages->send($userId, $message, 'uploadType' => 'resumable']);

$service->users_messages->send($userId, $message, 'uploadType' => 'multipart']);

still getting Error 413: Request Entity Too Large

Already researched on internet but not able to make it working.

Edit: Below codes give me Request is too large. even for 5 mb file

$mail->preSend();
$mime = $mail->getSentMIMEMessage();

$raw = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$message = new Google_Service_Gmail_Message();
$message->setRaw($raw);
$message->setThreadId($threadId); //only for reply
$sendOptions = [
     'uploadType' => 'resumable'
];
// size of chunks we are going to send to google
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$response = $service->users_messages->send($userId, $message, $sendOptions);
 // create mediafile upload
  $media = new Google_Http_MediaFileUpload(
          $client,
          $response,
          'message/rfc822',
          $raw,
          true,
          $chunkSizeBytes
   );
   $media->setFileSize(strlen($raw));
   // Upload the various chunks. $status will be false until the process is complete.
   $status = false;

    while (! $status) {
       $status = $media->nextChunk();
       echo $status ."<br>";
    }

     // Reset to the client to execute requests immediately in the future.
      $client->setDefer(false);

      // Get sent email message id
      $googleMessageId = $status->getId();

Here they suggest to Remove $message->setRaw($raw); . If I remove this line than I get Recipient address required error.

How I fixed it:

$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->addAddress($to);
$mail->AddCC($cc);
$mail->AddBCC($bcc);
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$sendOptions = [ 'uploadType' => 'resumable' ]; 
$client->setDefer(true);
$chunkSizeBytes = 1 * 1024 * 1024;
// create mediafile upload
$media = new Google_Http_MediaFileUpload(
          $client,
          $response,
          'message/rfc822',
          $mime,
          true,
          $chunkSizeBytes
      );
  $response = $service->users_messages->send($userId, $message);
  $media->setFileSize(strlen($mime));
      // 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.
      $client->setDefer(false);
      // Get sent email message id
      $googleMessageId = $status->getId();
softech
  • 356
  • 1
  • 4
  • 23

0 Answers0