contao/import — Issue #2659: Newsletter: Improve image includes
Author: importer
Created: 2011-11-29 19:56:08+00:00
State: open
Labels: Feature, newsletter

Images in the newsletter html content are automatically converted into inline attachments. This is done in system/libraries/Email.php near line 393. The regular expression matches all image-tags with 'src="'.

I think, it would be better, to match 'url(' too, to convert css-style-images into inline attachments.

-- original code line 393ff --

```
// Find images
$arrMatches = array();
preg_match_all('/src="([^"]+\.(jpe?g|png|gif|bmp|tiff?|swf))"/Ui', $this->strHtml, $arrMatches);
$strBase = Environment::getInstance()->base;

// Embed internal images
foreach (array_unique($arrMatches[1]) as $url)
{
// Try to remove the base URL
$src = str_replace($strBase, '', $url);

// Embed the image if the URL is now relative
if (

![](preg_match('`^https?://`', $src) && file_exists($this->strImageDir . $src))
{
$cid = $this->objMessage->embed(Swift_EmbeddedFile::fromPath($this->strImageDir . $src));
$this->strHtml = str_replace('src="' . $url . '"', 'src="' . $cid . '"', $this->strHtml);
}
}
```

-- replace with --

```
// Find images
$arrMatches = array();
preg_match_all('/(src="|url\(("|\'))([^"^\']+\.(jpe?g|png|gif|bmp|tiff?|swf))("|\')/Ui', $this->strHtml, $arrMatches);
$strBase = Environment::getInstance()->base;

// Embed internal images
foreach (array_unique($arrMatches[3]) as $key => $url)
{
// Try to remove the base URL
$src = str_replace($strBase, '', $url);

// Embed the image if the URL is now relative
if ()preg_match('`^https?://`', $src) && file_exists($this->strImageDir . $src))
{
$cid = $this->objMessage->embed(Swift_EmbeddedFile::fromPath($this->strImageDir . $src));
$arrSearch = array('src="' . $url . '"', 'url("' . $url . '"', 'url(\'' . $url . '\'');
$arrReplace = array('src="' . $cid . '"', 'url("' . $cid . '"', 'url(\'' . $cid . '\'');
$this->strHtml = str_replace($arrSearch, $arrReplace, $this->strHtml);
}
}
```

Related issues: #2396

--- _Originally created by ofriedrich on November 5th, 2010, at 02:16pm (ID 2659)_