[英]Gmail API with PHP mailer can't send multiple attachments
我正在將PHPmailer與Gmail API結合使用以發送郵件。 對於我來說,此過程非常適合僅發送標准電子郵件,但是,我還希望能夠使用Gmail API發送帶有附件的電子郵件。 當我嘗試使用$mail->addAttachment($urlString, $name);
Gmail會返回錯誤Request Entity Too Large Error 413
(附件的大小永遠不會超過20MB,因此應該在Gmail API的35MB限制之內)。
經過一番搜索,我發現這是因為我沒有使用“ / Upload 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);
}
我真的不知道從這里去哪里,所以我們將不勝感激。
使用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來部分構建電子郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.