gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:54:49 26 4
gpt4 key购买 nike

我需要帮助了解 Gmails API 以及如何修复下面的脚本。我浏览了 Stack 上的每一篇文章和 Gmail 上的所有文档,试图弄清楚如何使用 api 发送附件超过 5 Mb 的电子邮件。

这是我的脚本。如果附件小于 5Mb,此方法工作正常。它一结束,我就收到 413 错误。

Request Entity Too Large Error 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;
}

我不明白的是,在 Gmails 文档中,它说在上传附件时使用此 url。

 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)中没有任何地方可以使用该选项。

最佳答案

您在 google documentations 中发现了类似的错误尝试使用:分段上传:uploadType=multipart 或断点续传:uploadType=resumable

据推测这是应该如何工作的

# send the message...
$message = $gmail->users_messages->send("me", $msg, ['uploadType' => 'multipart']);

如果这里需要,api 引用 link

关于php - Gmail API PHP - 请求实体太大错误 413,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com