我正在使用帶有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);
}
我真的不知道從哪裏何去何從,所以任何幫助,將不勝感激。
爲gmail建立適當的電子郵件,以完全接受需要分配的時間我的解決方案是在500-2000線範圍內挑釁 – Ruttyj
聽起來不錯,我不得不看看。 –