PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Mail » Mail_Mime » Bug #4504

addHTMLImage does not work in cases when filename contains a path

Details

Request #4504addHTMLImage does not work in cases when filename contains a path
Submitted2005-06-01 22:48 UTC
Fromrichard at hyperlink dot net dot nz
Assignedcipri
StatusClosed
PackageMail_Mime
PHP VersionIrrelevant
OSIrrelevant
Roadmaps1.4.0, 1.4.0a1

Comments

[2005-06-01 22:48 UTC] richard at hyperlink dot net dot nz

Description:
------------
When called with a single argument, specifying the filename of the image, addHTMLImage does not work correctly if the image in the HTML is specified with a path.

E.g. when the HTML contains "<img src=images/mars.jpg>" and addHTMLImage("images/mars.jpg") is called, the image will not be inserted correctly.

This is because the function addHTMLImage truncates the filename to basename($file), and it is this truncated $file that is then later used when replacing the filename with the attachment CID (excuse me if my terminalogy is not correct).

I was going to suggest an alternate implementation, however this would probably break a lot of existing code. So more explicit documentation of what cases addHTMLImage is probably a better solution.

Since addHTMLImage also truncates the $name parameter, supplying the actual file contents in $file and the name in $name would still be problematic for my need, so I added the following function to the Mail_mime class.

This function will find all images in img tags and load all the files into the Mime_mail object. It takes a parameter $path which is prepended to the filenames, for when the files are not relative to the current working directory. You need to call addHTMLBody first. Note the regular expression only matches IMG tags where the SRC attribute is inside double quotes.

function findAddHTMLImages($path='')
{
preg_match_all('|<img .*src="(.+)".*>|Ui', $this->_htmlbody, $image_matches, PREG_PATTERN_ORDER);
$html_images=array_unique($image_matches[1]);
foreach ($html_images as $filename) {
$filedata=$this->_file2str($path.$filename);
if (PEAR::isError($filedata)) {
return $filedata;
}
$this->_html_images[] = array(
'body' => $filedata,
'name' => $filename,
'c_type' => 'application/octet-stream',
'cid' => md5(uniqid(time())));
}
return true;
}