我需要帮助理解Gmail的API以及如何修复下面的脚本。我已经阅读了Stack上的所有帖子和Gmail的所有文档,试图找出如何使用API发送一个超过5Mb的附件的方法。
这是我的脚本。如果附件小于5Mb,这个脚本可以正常工作。一旦超过5Mb,就会出现413错误。
我不明白的是,在Gmail文档中上传附件时使用的URL是什么。请注意,不要删除HTML标签。
这是我的脚本。如果附件小于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类中,根本没有可用的选项。