Browse Source

Fix embedded images in emails

Mark Scherer 10 years ago
parent
commit
80ecdb4478
2 changed files with 26 additions and 21 deletions
  1. 8 6
      Lib/EmailLib.php
  2. 18 15
      Test/Case/Lib/EmailLibTest.php

+ 8 - 6
Lib/EmailLib.php

@@ -155,6 +155,8 @@ class EmailLib extends CakeEmail {
 		if (empty($name)) {
 			$name = basename($file);
 		}
+
+		$name = pathinfo($name, PATHINFO_FILENAME) . '_' . md5($file) . '.' . pathinfo($name, PATHINFO_EXTENSION);
 		if ($contentId === null && ($cid = $this->_isEmbeddedAttachment($file, $name))) {
 			return $cid;
 		}
@@ -190,6 +192,8 @@ class EmailLib extends CakeEmail {
 			$ext = pathinfo($filename, PATHINFO_EXTENSION);
 			$mimeType = $this->_getMimeByExtension($ext);
 		}
+
+		$filename = pathinfo($filename, PATHINFO_FILENAME) . '_' . md5($content) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
 		if ($contentId === null && ($cid = $this->_isEmbeddedBlobAttachment($content, $filename))) {
 			return $cid;
 		}
@@ -210,6 +214,8 @@ class EmailLib extends CakeEmail {
 	 * to prevent the same image to overwrite each other and also to only send this image once.
 	 * Allows multiple usage of the same embedded image (using the same cid)
 	 *
+	 * @param string $file
+	 * @param string $name
 	 * @return string cid of the found file or false if no such attachment can be found
 	 */
 	protected function _isEmbeddedAttachment($file, $name) {
@@ -217,9 +223,7 @@ class EmailLib extends CakeEmail {
 			if ($filename !== $name) {
 				continue;
 			}
-			if ($fileInfo['file'] === $file) {
-				return $fileInfo['contentId'];
-			}
+			return $fileInfo['contentId'];
 		}
 		return false;
 	}
@@ -236,9 +240,7 @@ class EmailLib extends CakeEmail {
 			if ($filename !== $name) {
 				continue;
 			}
-			if ($fileInfo['content'] === $content) {
-				return $fileInfo['contentId'];
-			}
+			return $fileInfo['contentId'];
 		}
 		return false;
 	}

+ 18 - 15
Test/Case/Lib/EmailLibTest.php

@@ -281,14 +281,14 @@ class EmailLibTest extends MyCakeTestCase {
 		$this->assertContains('@' . env('HTTP_HOST'), $cid);
 
 		$res = $this->Email->getProtected('attachments');
+		$this->assertSame(1, count($res));
+		$image = array_shift($res);
 		$expected = [
-			'hotel.png' => [
-				'file' => $file,
-				'mimetype' => 'image/png',
-				'contentId' => $cid
-			]
+			'file' => $file,
+			'mimetype' => 'image/png',
+			'contentId' => $cid
 		];
-		$this->assertSame($expected, $res);
+		$this->assertSame($expected, $image);
 	}
 
 	/**
@@ -353,15 +353,17 @@ html-part
 
 		$this->assertContains('@' . env('HTTP_HOST'), $cid);
 
+		$cid2 = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
+		$this->assertSame($cid2, $cid);
+
 		$res = $this->Email->getProtected('attachments');
-		$expected = [
-			'my_hotel.png' => [
-				'content' => file_get_contents($file),
-				'mimetype' => 'image/png',
-				'contentId' => $cid,
-			]
-		];
-		$this->assertEquals($expected, $res);
+		$this->assertSame(1, count($res));
+
+		$cid3 = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file) . 'xxx', 'my_hotel.png');
+		$this->assertNotSame($cid3, $cid);
+
+		$res = $this->Email->getProtected('attachments');
+		$this->assertSame(2, count($res));
 
 		$options = [
 			'contentDisposition' => true,
@@ -370,13 +372,14 @@ html-part
 		$this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
 
 		$res = $this->Email->getProtected('attachments');
+		$keys = array_keys($res);
 		$expected = [
 			'contentDisposition' => true,
 			'content' => file_get_contents($file),
 			'mimetype' => 'image/jpeg',
 			'contentId' => $cid,
 		];
-		$this->assertEquals($expected, $res['my_other_hotel.png']);
+		$this->assertTrue($res[$keys[count($keys) - 1]]['contentDisposition']);
 	}
 
 	/**