From e28b12259fb40764ef658710c94f6bb110ba9c92 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 03 Nov 2013 09:05:39 -0500
Subject: [PATCH] Fix issue where mails with inline images of the same name contained only the first image multiple times (#1489406)

---
 program/steps/mail/sendmail.inc |   33 +++++++++++++++++++++++++--------
 1 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index ccb8978..52b02ec 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -615,22 +615,39 @@
 }
 
 // add stored attachments, if any
-if (is_array($COMPOSE['attachments']))
-{
+if (is_array($COMPOSE['attachments'])) {
   foreach ($COMPOSE['attachments'] as $id => $attachment) {
     // This hook retrieves the attachment contents from the file storage backend
     $attachment = $RCMAIL->plugins->exec_hook('attachment_get', $attachment);
 
-    $dispurl = '/\ssrc\s*=\s*[\'"]*\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\s\'"]*/';
-    $message_body = $MAIL_MIME->getHTMLBody();
-    if ($isHtml && (preg_match($dispurl, $message_body) > 0)) {
-      $message_body = preg_replace($dispurl, ' src="'.$attachment['name'].'" ', $message_body);
+    if ($isHtml) {
+      $dispurl      = '/\ssrc\s*=\s*[\'"]*\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\s\'"]*/';
+      $message_body = $MAIL_MIME->getHTMLBody();
+      $is_inline    = preg_match($dispurl, $message_body);
+    }
+    else {
+      $is_inline = false;
+    }
+
+    // inline image
+    if ($is_inline) {
+      // Mail_Mime does not support many inline attachments with the same name (#1489406)
+      // we'll generate cid: urls here to workaround this
+      $cid = preg_replace('/[^0-9a-zA-Z]/', '', uniqid(time(), true));
+      if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $from, $matches)) {
+        $cid .= $matches[1];
+      } else {
+        $cid .= '@localhost';
+      }
+
+      $message_body = preg_replace($dispurl, ' src="cid:' . $cid . '" ', $message_body);
+
       $MAIL_MIME->setHTMLBody($message_body);
 
       if ($attachment['data'])
-        $MAIL_MIME->addHTMLImage($attachment['data'], $attachment['mimetype'], $attachment['name'], false);
+        $MAIL_MIME->addHTMLImage($attachment['data'], $attachment['mimetype'], $attachment['name'], false, $cid);
       else
-        $MAIL_MIME->addHTMLImage($attachment['path'], $attachment['mimetype'], $attachment['name'], true);
+        $MAIL_MIME->addHTMLImage($attachment['path'], $attachment['mimetype'], $attachment['name'], true, $cid);
     }
     else {
       $ctype = str_replace('image/pjpeg', 'image/jpeg', $attachment['mimetype']); // #1484914

--
Gitblit v1.9.1