Skip to content

Commit

Permalink
- Implemented issue #13383: Add a method to extract/change/replace en…
Browse files Browse the repository at this point in the history
…tities in

  HTML mail with the CID elements replaced.
  • Loading branch information
derickr committed Oct 20, 2008
1 parent b6becb2 commit 344c7bb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 21 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.6alpha1 - [RELEASEDATE]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- Implemented issue #13383: Add a method to extract/change/replace entities in
HTML mail with the CID elements replaced.


1.5.2 - Monday 06 October 2008
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
Binary file modified design/class_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/tutorial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ For a list of supported mail-related RFCs, please see the `RFCs list`_.

.. _RFCs list: Mail_rfcs.html

Displaying HTML mail with inline images
---------------------------------------

In order to display HTML mail with inline images, you need to make sure that
the attached images are accessible by the web server. Besides this, you also
need to replace the internal mail references to the images with references to
the file in the document root. In the following example we use
ezcMailTools::replaceContentIdRefs() to do exactly this.

.. include:: tutorial/tutorial_html_display.php
:literal:


Troubleshooting
===============
Expand Down
42 changes: 42 additions & 0 deletions src/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,5 +683,47 @@ static public function guessContentType( $fileName, &$contentType, &$mimeType )
}
return true;
}

/**
* Replaces HTML embedded "cid:" references with replacements from $contentIdArray.
*
* The method matches all "cid:" references in the $htmlText and then loops
* over each match. For each match the found content ID is looked-up as key
* in the $contentIdArray and the value is then inserted as replacement for
* the "cid:" reference.
*
* <code>
* <?php
* $contentIdArray = array( 'consoletools-table.png@1421450' => 'http://localhost/consoletools-table.jpg' );
* $text = "<html> Embedded image: <img src='cid:consoletools-table.png@1421450'/> </html>";
* $htmlBody = ezcMailTools::replaceContentIdRefs( $text, $contentIdArray );
* // $htmlBody is now:
* // <html> Embedded image: <img src='http://localhost/consoletools-table.jpg'/> </html>
* ?>
* </code>
*
* The $contentIdArray can be build by iterating over all parts in the
* mail, and for each ezcMailFilePart that you find: 1. copy the associated
* file (fileName property of the ezcMailFilePart object) to your webroot;
* 2. add an element to the array with the key created from the contentId
* property from the ezcMailFilePart object. See the tutorial for an
* example of this.
*
* @param string $htmlText
* @param array(string=>string) $contentIdArray
* @return string
*/
static function replaceContentIdRefs( $htmlText, $contentIdArray )
{
preg_match_all( '@src=[\'"](cid:(.*?))[\'"]@', $htmlText, $matches );
for ( $i = 0; $i < count( $matches[0] ); $i++ )
{
if ( isset( $contentIdArray[$matches[2][$i]] ) )
{
$htmlText = str_replace( $matches[1][$i], $contentIdArray[$matches[2][$i]], $htmlText );
}
}
return $htmlText;
}
}
?>
58 changes: 37 additions & 21 deletions tests/tools_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ class ezcMailExtended extends ezcMail
*/
class ezcMailToolsTest extends ezcTestCase
{
/**
* Tests if ezcMailTools::composeEmailAddress works as it should
* @todo test if no 'email' is given.
*/
// Tests if ezcMailTools::composeEmailAddress works as it should
// @todo test if no 'email' is given.
public function testComposeEmailAddress()
{
$address = new ezcMailAddress( '[email protected]', 'John Doe' );
Expand All @@ -33,10 +31,8 @@ public function testComposeEmailAddress()
$this->assertEquals( '[email protected]', ezcMailTools::composeEmailAddress( $address ) );
}

/**
* Tests if ezcMailTools::composeEmailAddresses works as it should
* @todo test if no 'email' is given.
*/
// Tests if ezcMailTools::composeEmailAddresses works as it should
// @todo test if no 'email' is given.
public function testComposeEmailAddresses()
{
$addresses = array( new ezcMailAddress( '[email protected]', 'John Doe' ),
Expand Down Expand Up @@ -288,11 +284,9 @@ public function testValidateEmailAddressIncorrectMX()
}
}

/**
* Tests if generateContentId works as it should.
* Somewhat hard to test since it is supposed to return a unique string.
* We simply test if two calls return different strings.
*/
// Tests if generateContentId works as it should.
// Somewhat hard to test since it is supposed to return a unique string.
// We simply test if two calls return different strings.
public function testGenerateContentId()
{
if ( ezcMailTools::generateContentID() === ezcMailTools::generateContentID() )
Expand All @@ -301,11 +295,9 @@ public function testGenerateContentId()
}
}

/**
* Tests if generateMessageId works as it should.
* Somewhat hard to test since it is supposed to return a unique string.
* We simply test if two calls return different strings.
*/
// Tests if generateMessageId works as it should.
// Somewhat hard to test since it is supposed to return a unique string.
// We simply test if two calls return different strings.
public function testGenerateMessageId()
{
if ( ezcMailTools::generateMessageID( "doe.com" ) === ezcMailTools::generateMessageID( "doe.com") )
Expand All @@ -314,9 +306,6 @@ public function testGenerateMessageId()
}
}

/**
*
*/
public function testEndline()
{
// defaul is \n\r as specified in RFC2045
Expand Down Expand Up @@ -434,6 +423,33 @@ public function testGuessContentType()
}
}

public function testResolveCids()
{
$parser = new ezcMailParser();
$set = new ezcMailFileSet( array( dirname( __FILE__ )
. '/parser/data/various/test-html-inline-images' ) );
$mail = $parser->parseMail( $set );

$relatedParts = $mail[0]->body->getParts();
$alternativeParts = $relatedParts[0]->getParts();
$html = $alternativeParts[1]->getMainPart();

$convertArray = array(
'consoletools-table.png@1421450' => 'foo',
'consoletools-table.png@1421452' => 'bar'
);

$htmlBody = ezcMailTools::replaceContentIdRefs( $html->text, $convertArray );
$expected = <<<EOFE
<html>
Here is the HTML version of your mail
with an image: <img src='foo'/>
with an image: <img src='cid:consoletools-table.png@1421451'/>
</html>
EOFE;
self::assertSame( $expected, $htmlBody );
}

public static function suite()
{
return new PHPUnit_Framework_TestSuite( "ezcMailToolsTest" );
Expand Down

0 comments on commit 344c7bb

Please sign in to comment.