簡體   English   中英

如何使用Gmail API發送大附件

[英]How to send big attachments with gmail API

我正在嘗試使用Gmail API發送電子郵件。 我可以成功發送較小的文件。 當我嘗試發送更大尺寸的附件時出現問題。 我已經嘗試了兩個小時的解決方案了。 在給出錯誤413之前:請求實體太大 我更新了代碼,它看起來像這樣:

    $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();

現在,它給了我錯誤: 未捕獲的Google_Service_Exception:請求太大

順便說一句,我嘗試發送的文件是7.x MB,並且在創建啞劇消息后,整個啞劇消息的大小變為14MB左右,而API發送消息的限制為35 MB。 為什么給出Request是太大的錯誤。 請在這方面幫助我。

gmail檔案大小有限制,請嘗試上傳到Google雲端硬碟並分享

萬一其他人面臨同樣的問題。 我解決了我的問題。 我的程序中有錯誤。 我正在將編碼的$ mime傳遞給媒體構造函數。 它應該通過未編碼的啞劇。 為了使其正常運行,正在進行總體更改。

  1. 刪除了$ msg-> setRaw($ mime); 這條線。
  2. 將未解碼的$ mime傳遞給媒體構造函數。
  3. 將未解碼的mime大小傳遞給此行$ media-> setFileSize(strlen($ mime));

而且有效!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM