2017-06-22 23 views
0

我正在使用帶有Gmail API的PHPmailer發送郵件。這個過程對於我發送標準電子郵件非常有用,但是,我還希望能夠使用Gmail API發送帶有附件的電子郵件。當我嘗試使用$mail->addAttachment($urlString, $name);時,Gmail會返回錯誤Request Entity Too Large Error 413(附件大小永遠不會超過20MB,因此它應該在Gmail API的35MB範圍內)。使用PHP郵件程序的Gmail API無法發送多個附件

經過一番搜索之後,我發現這是因爲我沒有使用「/上傳URI」來發送大型Gmail附件(大於5MB且小於35MB)。問題是,我不是很瞭解如何使用Gmail API,只知道我現在從基本上從其他地方複製代碼並略微修改它的用途,因此我不知道如何更改URI那。

這裏是我到目前爲止,與標準的電子郵件的作品:

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

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

     $useremail = '[email protected]'; 
    $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); 

} 

我真的不知道從哪裏何去何從,所以任何幫助,將不勝感激。

回答

0

使用EZCMail並自己構建電子郵件結構......它非常適合Pickey!我可以在之後發佈一些細節。

您可能還必須以大塊的形式發送電子郵件。

如果電子郵件超過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部分構建電子郵件部分。

+0

爲gmail建立適當的電子郵件,以完全接受需要分配的時間我的解決方案是在500-2000線範圍內挑釁 – Ruttyj

+0

聽起來不錯,我不得不看看。 –