通过gmail api php代码发送邮件时,附件在Apple Mail客户端中重复出现

时间:2019-06-18 14:01:04

标签: php gmail-api apple-mail

我正在使用PHP中的Gmail Api代码向客户端发送邮件。该邮件包含多个附件,除了使用Apple Mail应用程序的客户端以外,其他所有附件均正常运行。他们看到重复的附件。也就是说,如果邮件具有4个不同的附件,则在Apple Mail应用程序中看到的所有邮件都是4个相同的重复附件。请在下面查看我的代码。

$objGMail = new Google_Service_Gmail($this->googleauth->client);
$strRawMessage = "";
$boundary = uniqid(rand(), true);
$subjectCharset = $charset = 'utf-8';

$strRawMessage .= 'To: ' .$toEmailsName . " <" . $ToEmail . ">" . "\r\n"; 
$strRawMessage .= 'Cc: ' .$ccEmailsName . " <" . $CcEmail . ">" . "\r\n";  
$strRawMessage .= 'Bcc: ' .$BccEmailsName . " <" . $BccEmail . ">" . "\r\n";
$strRawMessage .= 'From: '.$fromName . " <" . $fromEmail . ">" . "\r\n";   
$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($EmailSubject) . "?=\r\n";         
$strRawMessage .= 'MIME-Version: 1.0' . "\r\n";  
$finfo = finfo_open(FILEINFO_MIME_TYPE); 

//Attachments
if (isset($post['UploadedFileName'])) {
    for ($i = 0; $i < count($post['UploadedFileName']); $i++) {
        if ($post['UploadedFileName'][$i] <> "") {                                                                 
            $filePath = './attachments/' . $post['UploadedFileName'][$i];
            $mimeType = finfo_file($finfo, $filePath);                   
            $fileName = $post['UploadedFileName'][$i];                  
            $fileData = chunk_split(base64_encode(file_get_contents($filePath)), 76, "\n") . "\r\n";       

            $strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";                                                                                                  
            $strRawMessage .= "\r\n--{$boundary}\r\n";
            $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n";            
            $strRawMessage .= 'Content-ID: <' . $fromEmail . '>' . "\r\n";            
            $strRawMessage .= 'Content-Description: ' . $fileName . ';' . "\r\n";
            $strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "\r\n";
            $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";               
            $strRawMessage .=  $fileData;               
            $strRawMessage .= "--{$boundary}\r\n";                                                                                                 
        }
    }
}

$strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$strRawMessage .= $EmailBody . "\r\n";
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
$objSentMsg = $objGMail->users_messages->send("me", $msg);

0 个答案:

没有答案