使用PHP邮件程序的Gmail API无法发送多个附件

时间:2017-06-22 21:29:46

标签: php gmail-api

我正在使用PHPmailer和Gmail API发送邮件。这个过程对我发送标准电子邮件非常有用,但是,我还希望能够使用Gmail API发送带附件的电子邮件。当我尝试使用$mail->addAttachment($urlString, $name);时,Gmail会返回错误Request Entity Too Large Error 413(附件的大小永远不会超过20MB,因此它应该在Gmail API的35MB范围内。)

经过一番搜索后,我发现这是因为我没有使用“/上传URI”来发送大型Gmail附件(任何超过5mb且低于35mb的内容)。问题是,我对如何使用Gmail API并不是很了解,而且只从基本上从其他地方复制代码并稍微修改它以供我使用而得到我现在拥有的东西,因此,我不知道如何更改URI这一点。

以下是我目前使用的标准电子邮件:

function($agent, $assistantName='', $assistantEmail='', $subject, $body, $attachments=''){

$key = realpath(dirname(__FILE__).'/Services/Google/Gmail.json');

        $useremail = 'myuseremail@example.com';
    $toAddress = $agent->email;
    $agentFirst = $agent->first_name;


    $client = new Google_Client();
    putenv('GOOGLE_APPLICATION_CREDENTIALS='.$key);
    $client->useApplicationDefaultCredentials();
    $user_to_impersonate = $useremail;
    $client->setSubject($user_to_impersonate);
    $client->addScope('https://www.googleapis.com/auth/gmail.compose');
    if ($client->isAccessTokenExpired()) {
      $client->refreshTokenWithAssertion();
    }

        //prepare the mail with PHPMailer
        $mail = new PHPMailer();
        $mail->CharSet = "UTF-8";
        $mail->Encoding = "base64";
        $subject = $subject;
        $msg = $body;
        $from = $useremail;
        $fname = "My Name";
        $mail->From = $from;
        $mail->FromName = $fname;
        $mail->AddAddress($toAddress, $agentFirst);
        $mail->AddCC($assistantEmail, $assistantName);
        $mail->AddReplyTo($from,$fname);
        if ($attachments != '') {
          foreach ($attachments as $name => $urlString) {
            $mail->addAttachment($urlString, $name);
          }
        }
        $mail->Subject = $subject;
        $mail->Body = $msg;
        $mail->IsHTML(true);
        $mail->preSend();
        $mime = $mail->getSentMIMEMessage();
        $m = new Google_Service_Gmail_Message();
        $data = base64_encode($mime);
        $data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
        $m->setRaw($data);
        $service = new Google_Service_Gmail($client);
        $email = $service->users_messages->send($useremail, $m);
        return json_encode($email);

}

我真的不知道从哪里开始,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

使用EZCMail并自己构建电子邮件结构......这非常有用!我可以发布一些细节。

您可能还需要以块的形式发送电子邮件。

如果电子邮件超过4MB,那么您将不得不使用Google_Http_MediaFileUpload发送数据块

使用此代码的代码应与此类似,有一些示例可以在网络上的其他地方使用Google_Http_MediaFileUpload,这可能更完整:

                    $client->setDefer(true);
                    // Call the API with the media upload, defer so it doesn't immediately return.
                    $arrRequestParams = $arrRequestParams+['uploadType' => 'multipart'];
                    $result = $this->TransportPrepSwitch($strAction, $objGMessage, $arrRequestParams); // Make draft or message $service->users_messages->send('me', $objGMessage, $arrRequestParams);
                    $mailMessage = base64url_decode($strRaw);
                    // create mediafile upload
                    $chunkSizeBytes = 1*1024*1024; // send to google in 1MB chunks
                    $media = new Google_Http_MediaFileUpload($client,$result,'message/rfc822',$mailMessage,true,$chunkSizeBytes);
                    $media->setFileSize(strlen($mailMessage));
                    // Upload chunks. Status will contain the completed $result.
                    $status = false;
                    set_time_limit(300);
                    while(!$status)
                        $status = $media->nextChunk();
                    // Reset to the client to execute requests immediately in the future.
                    $client->setDefer(false);
                    $objResponce = $status;

电子邮件部分的结构也必须如下:

multipart/mixed => [
        multipart/related => [
            multipart/alternative => [
                plain,
                html
            ],
            inline images,
        ],
        attachments,
    ]

我能实现这一目标的唯一方法是使用EZCMail按部分构建电子邮件。