Gmail API PHP - 请求实体太大错误 413 [英] Gmail API PHP - Request Entity Too Large Error 413

查看:35
本文介绍了Gmail API PHP - 请求实体太大错误 413的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的脚本.如果附件小于 5Mb,这可以正常工作.它一过去,我就收到 413 错误.

<块引用>

请求实体太大错误 413

 # 设置谷歌客户端...$client = new Google_Client();$client->setApplicationName(我的应用程序");$client->setAuthConfig($array['credentials']);# 创建新的 Gmail 服务...$gmail = new \Google_Service_Gmail($client);# 设置内容...$strRawMessage = "";$boundary = uniqid(rand(), true);$subjectCharset = $charset = 'utf-8';$strMailContent = '测试邮件正文...';$strMailContent =quoted_printable_encode( $strMailContent );$strSubject = '测试消息主题...';# 设置消息发送给谁...$to[] = $this->encodeRecipients('You' . " <you@gmail.com>>");$strRawMessage .= "收件人:".内爆(", ", $to) .\r\n";# 设置主题...$strRawMessage .= '主题:=?'.$subjectCharset .?B?".base64_encode($strSubject) .?=\r\n";$strRawMessage .= 'MIME-Version: 1.0' .\r\n";$strRawMessage .= '内容类型:多部分/混合;边界="'.$边界.'''.\r\n";# 设置身体...$strRawMessage .= "\r\n--{$boundary}\r\n";$strRawMessage .= '内容类型:text/html;字符集 = = .$字符集.\r\n";$strRawMessage .= '内容传输编码:引用打印'.\r\n\r\n";$strRawMessage .= $strMailContent .\r\n";# 遍历附件...$附件[] = ['url'=>'https://s3-us-west-2.amazonaws.com/xyz/bugfiles/Pizigani_1367_Chart_10MB.jpg','name'='Pizigani_1367_Chart_10MB.jpg','大小'='10Mb'];foreach($attachments 作为 $attachment){# 获取文件信息...$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'];# 将其添加为电子邮件的附件...$strRawMessage .= "\r\n--{$boundary}\r\n";$strRawMessage .= '内容类型:'.$mimeType .';名称=".$文件名.'";'.\r\n";$strRawMessage .= '内容描述:' .$文件名.';'.\r\n";$strRawMessage .= '内容处理:附件;文件名="'.$attachment['name'] .'-' .$文件名.'";大小=.$文件大小.';'.\r\n";$strRawMessage .= '内容传输编码:base64'.\r\n\r\n";$strRawMessage .= chunk_split(base64_encode(file_get_contents($url)), 76, "\n") .\r\n";$strRawMessage .= '--' .$边界.\r\n";}尝试 {# 在 message/rfc822 中准备消息$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');$msg = new \Google_Service_Gmail_Message();$msg->setRaw($mime);# 发送消息...$message = $gmail->users_messages->send(me", $msg);echo '

';打印_r($消息);echo '</pre>';死;} catch (\Exception $e) {echo '

';print_r($e->getMessage());echo '</pre>';死;}

我不明白的是,在 Gmails 文档中,它说上传附件时要使用此网址.

 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)中的任何地方都无法使用该选项.

解决方案

您在 google 文档 尝试使用:分段上传:uploadType=multipart 或可恢复上传:uploadType=resumable

据说这应该是如何工作的

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

如果需要这里的 api 参考 link

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.

Here is my script. This works fine if the attachments are less than 5Mb. The instant it goes over, I get the 413 error.

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;
    }

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

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

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.

解决方案

You have a similar error as found here in the google documentations Try using: Multipart upload: uploadType=multipart or Resumable upload: uploadType=resumable

and supposedly this is how should work

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

if needed here the api reference link

这篇关于Gmail API PHP - 请求实体太大错误 413的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆