/** * {@inheritdoc) */ public function transform(\Swift_Mime_Message $message) { $body = $message->getBody(); $body = preg_replace_callback('/(src|background)="(http[^"]*)"/', function ($matches) use($message) { $attribute = $matches[1]; $imagePath = $matches[2]; if ($fp = fopen($imagePath, "r")) { $imagePath = $message->embed(\Swift_Image::fromPath($imagePath)); fclose($fp); } return sprintf('%s="%s"', $attribute, $imagePath); }, $body); $body = preg_replace_callback('/url\\((http[^"]*)\\)/', function ($matches) use($message) { $imagePath = $matches[1]; if ($fp = fopen($imagePath, "r")) { $imagePath = $message->embed(\Swift_Image::fromPath($imagePath)); fclose($fp); } return sprintf('url(%s)', $imagePath); }, $body); $message->setBody($body, 'text/html'); }
/** * {@inheritdoc} */ public function embedData(string $data, string $name, string $contentType = null) : string { $image = Swift_Image::newInstance($data, $name, $contentType); return $this->swift->embed($image); }
/** * Embed pictures * * @param \Swift_Mime_Message $message E-mail message * * @access public * @return void * @author Etienne de Longeaux <*****@*****.**> */ public function pictureEmbed(\Swift_Mime_Message &$message) { $body = $message->getBody(); $body = preg_replace_callback('/(src|background)="(http[^"]*)"/', function ($matches) use($message) { $attribute = $matches[1]; $imagePath = $matches[2]; if ($fp = fopen($imagePath, "r")) { // You can embed files from a URL if allow_url_fopen is on in php.ini $imagePath = $message->embed(\Swift_Image::fromPath($imagePath)); fclose($fp); } return sprintf('%s="%s"', $attribute, $imagePath); }, $body); $body = preg_replace_callback('/url\\((http[^"]*)\\)/', function ($matches) use($message) { $imagePath = $matches[1]; if ($fp = fopen($imagePath, "r")) { // You can embed files from a URL if allow_url_fopen is on in php.ini $imagePath = $message->embed(\Swift_Image::fromPath($imagePath)); fclose($fp); } return sprintf('url(%s)', $imagePath); }, $body); $message->setBody($body, 'text/html'); }