Gmail API PHP-请求实体太大错误413 [英] Gmail API PHP - Request Entity Too Large Error 413
问题描述
我需要帮助来了解Gmails API以及如何在下面修复脚本.我遍历了Stack上的所有帖子以及Gmail上的所有文档,以尝试弄清楚如何使用具有5 Mb以上附件的api发送电子邮件.
I need help understanding Gmails API and how I can fix my script below. I have gone through every post on Stack and all the documentation on Gmail to try and figure out how to send an email using the api with attachments over 5 Mb.
这是我的剧本.如果附件小于5Mb,则可以正常工作.它过去的那一刻,我得到413错误.
Here is my script. This works fine if the attachments are less than 5Mb. The instant it goes over, I get the 413 error.
请求实体太大错误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;
}
我不明白的是,在Gmail文档中,它说在上传附件时使用此URL.
What I do not understand is that in Gmails docs it says to use this url when uploading attachments.
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=multipart
或者说使用此URL更加可靠...
Or it says to use this URL to be more reliable...
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=resumable
我看不到的是什至我甚至都不会使用该URL的地方.在类(Google_Client,Google_Service_Gmail,Google_Service_Gmail_Message)中没有哪个位置甚至可以使用该选项.
What I dont see is where I would even use that URL at all. No where in the classes (Google_Client, Google_Service_Gmail, Google_Service_Gmail_Message) is that option even available to use.
如果您具备使我的脚本发送25 mb附件所需的技能,我将愿意付钱帮助我解决此问题.
I would be willing to pay someone to help me remedy this issue if you have the skill set necessary to make my script send a 25 mb attachment.
谢谢.
推荐答案
You have a similar error as found here in the google documentations Try using:
Multipart upload: uploadType=multipart
or Resumable upload: uploadType=resumable
并且应该是这样工作
# send the message...
$message = $gmail->users_messages->send("me", $msg, ['uploadType' => 'multipart']);
如果需要,此处提供api参考链接
if needed here the api reference link
这篇关于Gmail API PHP-请求实体太大错误413的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!