Created
November 18, 2014 15:42
-
-
Save voku/0a347ae006eee786359e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Mockery as m; | |
class Swift_Bug543Test extends \PHPUnit_Framework_TestCase | |
{ | |
public $message = ''; | |
public $mailHtmlBody = ''; | |
public function embeddingImages() | |
{ | |
if ($this->mailHtmlBody == '') { | |
return false; | |
} | |
$possibleImagesFormat = array( | |
'jpg', | |
'jpeg', | |
'png', | |
'gif' | |
); | |
$imageEmbedArray = array(); | |
$htmlTmp = str_get_html($this->mailHtmlBody); | |
if (!$htmlTmp instanceof simple_html_dom) { | |
return false; | |
} | |
// Retrieve all img src tags and replace them | |
foreach ($htmlTmp->find('img') as $e) { | |
if ($e->src != "") { | |
if (in_array($this->getFileExtension($e->src), $possibleImagesFormat, true)) { | |
// check if we already used this image | |
if (isset($imageEmbedArray[$e->src])) { | |
// replace the image | |
$e->src = $imageEmbedArray[$e->src]; | |
} else { | |
// create a new embed-image | |
$newValue = $this->message->embed(Swift_Image::fromPath($e->src)); | |
// save old && new value to the array | |
$imageEmbedArray[$e->src] = $newValue; | |
// replace the image | |
$e->src = $newValue; | |
} | |
} | |
} | |
} | |
// Retrieve all background tags and replace them | |
foreach ($htmlTmp->find('td') as $e) { | |
if ($e->background != "") { | |
if (in_array(getFileExtension($e->src), $possibleImagesFormat, true)) { | |
// check if we already used this image | |
if (isset($imageEmbedArray[$e->background])) { | |
// replace the image | |
$e->background = $imageEmbedArray[$e->background]; | |
} else { | |
// create a new embed-image | |
$newValue = $this->message->embed(Swift_Image::fromPath($e->background)); | |
// save old && new value to the array | |
$imageEmbedArray[$e->src] = $newValue; | |
// replace the image | |
$e->background = $newValue; | |
} | |
} | |
} | |
} | |
$this->mailHtmlBody = $htmlTmp; | |
unset($htmlTmp); | |
return true; | |
} | |
/** | |
* get file-extension | |
* | |
* @param String $filename | |
* | |
* @return String | false (by error) | |
*/ | |
public function getFileExtension($filename) | |
{ | |
$ext = false; | |
if (function_exists('pathinfo') && (file_exists($filename))) { | |
$ext = pathinfo($filename, PATHINFO_EXTENSION); | |
} else { | |
if (stristr($filename, '.') !== false) { | |
$a = explode('.', $filename); | |
$ext = array_pop($a); | |
} | |
} | |
if ($ext !== false) { | |
$ext = strtolower($ext); | |
} | |
return $ext; | |
} | |
public function testEmbeddedImagesAreEmbedded() | |
{ | |
date_default_timezone_set('Europe/Berlin'); | |
$recipient = array('test@test.de'); | |
$this->message = new Swift_Message(); | |
$this->mailHtmlBody = 'Look at <b>this</b> image: <img alt="Image" width="181" height="68" src="http://swiftmailer.org/images/logo.png" />'; | |
$this->embeddingImages(); | |
$this->message->setBody($this->mailHtmlBody, 'text/html'); | |
$this->message->setTo(array('test@test.de')); | |
$messageNew = clone $this->message; | |
$messageNew->setBody($this->mailHtmlBody, 'text/html'); | |
//$transport = Swift_MailTransport::newInstance('/usr/sbin/sendmail'); | |
//$mailer = Swift_Mailer::newInstance($transport); | |
// ok | |
//var_dump($this->message); | |
// not ok | |
//var_dump($messageNew); | |
preg_match("/cid:(.*)\"/", $this->message->toString(), $imageCIDs); | |
$imageCID = $imageCIDs[1]; | |
preg_match("/Content-ID: <(.*)>/", $this->message->toString(), $imageContentID); | |
$imageContentID = $imageContentID[1]; | |
$this->assertEquals($imageCID, $imageContentID); | |
preg_match("/cid:(.*)\"/", $messageNew->toString(), $imageCIDs); | |
$imageCID = $imageCIDs[1]; | |
preg_match("/Content-ID: <(.*)>/", $messageNew->toString(), $imageContentID); | |
$imageContentID = $imageContentID[1]; | |
$this->assertEquals($imageCID, $imageContentID); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment