|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23530 Mail_Mime image URL replacement error
Submitted: 2003-05-07 12:33 UTC Modified: 2004-12-13 15:47 UTC
From: pear-bugs-NOSPAM-7may2003 at ryandesign dot com Assigned: gschlossnagle (profile)
Status: Not a bug Package: PEAR related
PHP Version: 4.3.2RC1 OS: Linux
Private report: No CVE-ID: None
 [2003-05-07 12:33 UTC] pear-bugs-NOSPAM-7may2003 at ryandesign dot com
Hello. I've been spending the day playing with sending MIME email with included HTML images, using the Mail_Mime part of PEAR. I've run into two bugs, which both have the same fix.

Suppose I try to send an HTML mail with the following source:

    <img src="image.gif">
    This image is called image.gif.
    <img src="another_image.gif">
    This image is called another_image.gif.

Suppose that I attach the two images using addHTMLImage, and that I do so in the order in which they're specified in the source -- "image.gif" first, "another_image.gif" second. When I send the message, the HTML will have been translated like, for example, this:

    <img src=3D"cid:6a7258f7707824eb9df3e93d8983fc64">
    This image is called cid:6a7258f7707824eb9df3e93d8983fc64.
    <img src=3D"another_cid:6a7258f7707824eb9df3e93d8983fc64">
    This image is called another_cid:6a7258f7707824eb9df3e93d8983fc64.

You can immediately see that there are two things wrong. First, it has replaced occurrences of the image name within the text with the CID tag. This should not be done. It should only replace the image names where they occur within a "src" or "background" attribute of a tag, or a "url" function in a stylesheet, or similar.

Second, you can see that because the name of the first image was contained within the name of the second image, the second image has been destroyed.

Here's the culprit. At the beginning of the "get" function in Mime.php there are lines that look like this:

        if (!empty($this->_html_images) AND isset($this->_htmlbody)) {
            foreach ($this->_html_images as $value) {
                $this->_htmlbody = str_replace($value['name'], 'cid:'.$value['cid'], $this->_htmlbody);

The replace is much too general. I would suggest replacing using a regular expression. Something like this:

                $this->_htmlbody = preg_replace(
                    '/(\s+(?:src|background))\s*=\s*(?:"' . $value['name'] . '"|' . $value['name'] . ')(?=[\s>])/si',
                    '$1="cid:' . $value['cid'] . '"',

Of course this doesn't take care of the "url" function notation that can be used in CSS and there may be other considerations.... but I think it's a better method than the one currently in place.

For now, users can use a workaround for the second point: make an array of images you want to add, sort it in descending order by length, and then use the addHTMLImage function on each element of the array.

You can email me at pear bug report 7 may 2003 at ryan design dot com.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-26 08:56 UTC]
who is maintaining Mail_Mime now ?
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Dec 01 12:01:26 2023 UTC