I am using the Gmail Api code in php to send mails to clients. The mail contains multiple attachments and all works good except for the clients who are using the apple mail app. They see the duplicated attachments. That is if the mail has 4 different attachments, all they see in apple mail app is 4 same duplicated attachments. Please see my code below.
$objGMail = new Google_Service_Gmail($this->googleauth->client);
$strRawMessage = "";
$boundary = uniqid(rand(), true);
$subjectCharset = $charset = 'utf-8';
$strRawMessage .= 'To: ' .$toEmailsName . " <" . $ToEmail . ">" . "
";
$strRawMessage .= 'Cc: ' .$ccEmailsName . " <" . $CcEmail . ">" . "
";
$strRawMessage .= 'Bcc: ' .$BccEmailsName . " <" . $BccEmail . ">" . "
";
$strRawMessage .= 'From: '.$fromName . " <" . $fromEmail . ">" . "
";
$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($EmailSubject) . "?=
";
$strRawMessage .= 'MIME-Version: 1.0' . "
";
$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, "
") . "
";
$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "
";
$strRawMessage .= "
--{$boundary}
";
$strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "
";
$strRawMessage .= 'Content-ID: <' . $fromEmail . '>' . "
";
$strRawMessage .= 'Content-Description: ' . $fileName . ';' . "
";
$strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "
";
$strRawMessage .= 'Content-Transfer-Encoding: base64' . "
";
$strRawMessage .= $fileData;
$strRawMessage .= "--{$boundary}
";
}
}
}
$strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "
";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "
";
$strRawMessage .= $EmailBody . "
";
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
$objSentMsg = $objGMail->users_messages->send("me", $msg);