Gmail API PHP - 请求实体过大错误 413

3
我需要帮助理解Gmail的API以及如何修复下面的脚本。我已经阅读了Stack上的所有帖子和Gmail的所有文档,试图找出如何使用API发送一个超过5Mb的附件的方法。
这是我的脚本。如果附件小于5Mb,这个脚本可以正常工作。一旦超过5Mb,就会出现413错误。

请求实体过大错误413

    # set up the google client...
    $client = new Google_Client();
    $client->setApplicationName("My App");
    $client->setAuthConfig($array['credentials']);
    
    # create new gmail service...
    $gmail = new \Google_Service_Gmail($client);
    
    # set the content...
    $strRawMessage = "";
    $boundary = uniqid(rand(), true);
    $subjectCharset = $charset = 'utf-8';
    $strMailContent = 'Test Message Body...';
    $strMailContent = quoted_printable_encode( $strMailContent );
    $strSubject = 'Test Message Subject...';
    
    # set up who the message is being sent to...
    $to[] = $this->encodeRecipients('You' . " <you@gmail.com>");
    $strRawMessage .= "To: " . implode(", ", $to) . "\r\n";
    
    # set the subject...
    $strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
    $strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
    $strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";
    
    # set the body...
    $strRawMessage .= "\r\n--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= $strMailContent . "\r\n";
    
    # loop over the attachments...
    $attachments[] = [
        'url'=>'https://s3-us-west-2.amazonaws.com/xyz/bugfiles/Pizigani_1367_Chart_10MB.jpg',
        'name'=>'Pizigani_1367_Chart_10MB.jpg',
        'size'=>'10Mb'
    ];
    foreach($attachments as $attachment){
    
        # get the file info...
        $url = $attachment['url'];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);
        $mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
        $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
        $path = parse_url($url, PHP_URL_PATH);
        $filename = substr($path, strrpos($path, '/') + 1); # $attachment['name'];
    
        # add it as an attachment to the email...
        $strRawMessage .= "\r\n--{$boundary}\r\n";
        $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $filename .'";' . "\r\n";
        $strRawMessage .= 'Content-Description: ' . $filename . ';' . "\r\n";
        $strRawMessage .= 'Content-Disposition: attachment; filename="' . $attachment['name'] . '-' . $filename . '"; size=' . $fileSize. ';' . "\r\n";
        $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
        $strRawMessage .= chunk_split(base64_encode(file_get_contents($url)), 76, "\n") . "\r\n";
        $strRawMessage .= '--' . $boundary . "\r\n";
    }
    
    try {
    
        # Prepare the message in message/rfc822
        $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
        $msg = new \Google_Service_Gmail_Message();
        $msg->setRaw($mime);
    
        # send the message...
        $message = $gmail->users_messages->send("me", $msg);
    
        echo '<pre>';
        print_r($message);
        echo '</pre>';
        die;
    
    } catch (\Exception $e) {
        echo '<pre>';
        print_r($e->getMessage());
        echo '</pre>';
        die;
    }

我不明白的是,在Gmail文档中上传附件时使用的URL是什么。请注意,不要删除HTML标签。
 POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=multipart

或者,它建议使用此URL以提高可靠性...

 POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=resumable

我看不出来在哪里使用那个URL。在Google_Client、Google_Service_Gmail和Google_Service_Gmail_Message类中,根本没有可用的选项。

2个回答

1
你遇到的错误与谷歌文档中发现的相似。尝试使用:多部分上传:uploadType=multipart或可恢复上传:uploadType=resumable 据说这是应该如何工作的。
# send the message...
$message = $gmail->users_messages->send("me", $msg, ['uploadType' => 'multipart']);

如果需要,这里有 API 参考 link。保留 HTML 格式,不做解释。

0

Google API 对某些事情非常严格。您正在使用的方法(users/SendMessage)的限制是35MB,这是正确的方法,根据文档。实际上,使用 MIME TYPE multipart 进行发布,在某些情况下(几乎所有情况),会将文件大小扩大一倍。例如,如果您上传一个20MB的文件,它最终可能会变成接近40MB,因此 Google 返回该错误,因为最终他们得到的是一个超过文件大小限制的文件。


你是在建议我删除这行代码吗:$strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $filename .'";' . "\r\n"; - LargeTuna
不,一切似乎都没问题...问题在于你(以及每个人)需要如何向Google API发布数据。我们需要使用MIME类型multipart,这本身几乎会使文件大小增加一倍...这是一个可怕的限制,所以您无法上传任何大于(我认为)13MB的文件。 - Optiroot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接