我需要帮助了解Gmails API以及如何在下面修复我的脚本。我已经浏览了Stack上的每个帖子和Gmail上的所有文档,试图找出如何使用超过5 Mb附件的api发送电子邮件。
这是我的脚本。如果附件小于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;
}
我不明白的是,在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
我看不到的是我甚至会使用该网址。不在课程中的哪个位置(Google_Client,Google_Service_Gmail,Google_Service_Gmail_Message)甚至可以使用该选项。
如果您具备使我的脚本发送25 MB附件所需的技能,我愿意付钱帮助我解决此问题。
感谢。
答案 0 :(得分:1)
您遇到与Google documentations 中发现的类似错误。请尝试使用:
分段上传:uploadType=multipart
或可恢复上传:uploadType=resumable
并且据说这应该是如何工作的
# send the message...
$message = $gmail->users_messages->send("me", $msg, ['uploadType' => 'multipart']);
如果需要api参考link
答案 1 :(得分:0)
Google API对某些内容非常严格。您正在使用的方法(users / SendMessage)的限制是正确的,根据documentation限制为35MB。实际上使用MIME TYPE multipart发布,在某些情况下(几乎所有)将文件大小扩展为double。例如,如果您上传的文件大小为20MB,则最终可能会达到40MB左右,因此Google会返回该错误,因为最终他们会收到大于文件大小限制的文件。