From c93759dcd19b43a1fb905dc83dec51d1287b24bc Mon Sep 17 00:00:00 2001 From: Synchro Date: Tue, 25 Aug 2015 12:01:39 +0200 Subject: [PATCH] Fix issues with embedding items without filenames, fixes #478 --- class.phpmailer.php | 45 +++++++++++++++++++++++++++++------------- test/phpmailerTest.php | 25 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/class.phpmailer.php b/class.phpmailer.php index 34c2ea1d..94a50a9d 100644 --- a/class.phpmailer.php +++ b/class.phpmailer.php @@ -2330,12 +2330,21 @@ class PHPMailer $cidUniq[$cid] = true; $mime[] = sprintf('--%s%s', $boundary, $this->LE); - $mime[] = sprintf( - 'Content-Type: %s; name="%s"%s', - $type, - $this->encodeHeader($this->secureHeader($name)), - $this->LE - ); + //Only include a filename property if we have one + if (!empty($name)) { + $mime[] = sprintf( + 'Content-Type: %s; name="%s"%s', + $type, + $this->encodeHeader($this->secureHeader($name)), + $this->LE + ); + } else { + $mime[] = sprintf( + 'Content-Type: %s%s', + $type, + $this->LE + ); + } // RFC1341 part 5 says 7bit is assumed if not specified if ($encoding != '7bit') { $mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, $this->LE); @@ -2359,12 +2368,20 @@ class PHPMailer $this->LE . $this->LE ); } else { - $mime[] = sprintf( - 'Content-Disposition: %s; filename=%s%s', - $disposition, - $encoded_name, - $this->LE . $this->LE - ); + if (!empty($encoded_name)) { + $mime[] = sprintf( + 'Content-Disposition: %s; filename=%s%s', + $disposition, + $encoded_name, + $this->LE . $this->LE + ); + } else { + $mime[] = sprintf( + 'Content-Disposition: %s%s', + $disposition, + $this->LE . $this->LE + ); + } } } else { $mime[] = $this->LE; @@ -2801,7 +2818,7 @@ class PHPMailer $disposition = 'inline' ) { // If a MIME type is not specified, try to work it out from the name - if ($type == '') { + if ($type == '' and !empty($name)) { $type = self::filenameToType($name); } @@ -3103,7 +3120,7 @@ class PHPMailer $data = rawurldecode($data); } $cid = md5($url) . '@phpmailer.0'; // RFC2392 S 2 - if ($this->addStringEmbeddedImage($data, $cid, '', 'base64', $match[1])) { + if ($this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, 'base64', $match[1])) { $message = str_replace( $images[0][$imgindex], $images[1][$imgindex] . '="cid:' . $cid . '"', diff --git a/test/phpmailerTest.php b/test/phpmailerTest.php index 711e9c3e..4d9153a3 100644 --- a/test/phpmailerTest.php +++ b/test/phpmailerTest.php @@ -975,6 +975,31 @@ EOT; $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo); } + /** + * Test embedded image without a name + */ + public function testHTMLStringEmbedNoName() + { + $this->Mail->Body = 'This is the HTML part of the email.'; + $this->Mail->Subject .= ': HTML + unnamed embedded image'; + $this->Mail->isHTML(true); + + if (!$this->Mail->addStringEmbeddedImage( + file_get_contents('../examples/images/phpmailer_mini.png'), + md5('phpmailer_mini.png').'@phpmailer.0', + '', //intentionally empty name + 'base64', + 'image/png', + 'inline') + ) { + $this->assertTrue(false, $this->Mail->ErrorInfo); + return; + } + + $this->buildBody(); + $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo); + } + /** * Simple HTML and multiple attachment test */