Reputation: 11
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 . ">" . "\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);
Upvotes: 0
Views: 125
Reputation: 1
I had the same issue but not specifically due to the PHP API. I have seen it is related to Gmail API's across the board. Gmail adds a blank Content-Id like this: "Content-ID: <>". So if you have multiple attachments and no Content-Id is supplied then both will be seen by Apple Mail as the same attachment. All other large clients like outlook and gmail handle this scenario fine. My recommendation is to set unique content-id's when attaching files.
To test it out I opened the .eml with the duplicate attachements and removed the "Content-ID: <>" for both attachments and then reopened the .eml in Apple mail and both correct attachments appeared.
Upvotes: 0