【发布时间】:2018-05-20 14:50:28
【问题描述】:
我需要帮助来了解 Gmails API 以及如何在下面修复我的脚本。我浏览了 Stack 上的每一篇文章和 Gmail 上的所有文档,试图弄清楚如何使用 api 发送电子邮件,附件超过 5 Mb。
这是我的脚本。如果附件小于 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 文档中,它说在上传附件时使用此网址。
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=multipart
或者说使用这个网址更可靠...
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=resumable
我根本没有看到我什至会在哪里使用该 URL。在类(Google_Client、Google_Service_Gmail、Google_Service_Gmail_Message)中没有任何地方可以使用该选项。
【问题讨论】:
标签: php api email google-api gmail-api