2017-06-22 12 views
0

Gmail APIを使用してPHPmailerを使用してメールを送信しています。このプロセスは標準のメールだけを送信するのにはうまくいきましたが、Gmail APIを使用して添付ファイル付きのメールを送信したいと考えています。 $mail->addAttachment($urlString, $name);を使用してみると、エラーRequest Entity Too Large Error 413が返されます(添付ファイルのサイズは20MBを超えることはありませんので、Gmail APIの35MBの制限内に収まるはずです)。PHPメーラーを使用したGmail APIは複数の添付ファイルを送信できません

検索の結果、大容量のGmail添付ファイル(5MB以上35MB未満のもの)を送信するためには「/アップロードURI」を使用していなかったことがわかりました。問題は、私は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を使用して、自分でメール構造を作成してください...それは非常にピックです!私は後にいくつかの詳細を投稿することができます。

また、チャンクで電子メールを送信する必要があります。

メールが4メガバイトを超えている場合、あなたはこれを使用して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, 
    ] 

T彼は私がこれを達成できる唯一の方法はEZCMailを使って電子メールを部分的に構築することでした。

+0

gmailのメールを完全に受け入れるには時間がかかります。私の解決策は、500-2000の範囲で挑戦的です。 – Ruttyj

+0

良い気分です。 –

関連する問題