How to send big attachments with gmail API

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 12:36:30

问题


i'm trying to send an email with Gmail API. I'm able to send it with smaller files successfully. Problem appears when I try to send attachment with bigger size. It's been couple of hours I'm trying different solutions. Before it was giving error Error 413: Request Entity Too Large. I updated my code and it looks like this:

    $mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
$sendOptions = [
        'uploadType' => 'resumable'
];
// size of chunks we are going to send to google
$chunkSizeBytes = 1 * 1024 * 1024;

$client->setDefer(true);
$objSentMsg = $service->users_messages->send('me', $msg, $sendOptions);

// create mediafile upload
$media = new Google_Http_MediaFileUpload(
    $client,
    $objSentMsg,
    'message/rfc822',
    $mime,
    true,
    $chunkSizeBytes
);
//I tried to pass null in above media object in place of $mime variable. didn't worked either.


$media->setFileSize(strlen($mime));

// 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 = $result->getId();

And now it is giving me error: Uncaught Google_Service_Exception: Request is too large

By the way the file i'm trying to send is 7.x MB and after mime message creation the size of whole mime message becomes around 14MB and API limitation to send message is 35 MB. Why it is giving Request is too large error. Please help me in this regard.


回答1:


Well gmail has a file size limit try uploading to the Google drive and share that




回答2:


In case anyone else is facing same problem. I solved my problem. There was bug in my program. I was passing encoded $mime to media constructor. It should be pass un-encoded mime. Overall changes were following to make it working.

  1. Removed $msg->setRaw($mime); this line.
  2. Passed un-decoded $mime to media constructor.
  3. Passed un-decoded mime size to this line $media->setFileSize(strlen($mime));

And it worked!!



来源:https://stackoverflow.com/questions/50805550/how-to-send-big-attachments-with-gmail-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!