Author: Alexandru Stanoi Date: 2007-01-09 11:11:01 +0100 (Tue, 09 Jan 2007) New Revision: 4475
Log: - Added ezcMailTools::guessContentType to resolve a bug in which the images embeded in an html part were treated like application/octet-stream attachments. Modified: trunk/Mail/ChangeLog trunk/Mail/src/composer.php trunk/Mail/src/tools.php trunk/Mail/tests/composer_test.php trunk/Mail/tests/tools_test.php Modified: trunk/Mail/ChangeLog =================================================================== --- trunk/Mail/ChangeLog 2007-01-09 09:12:33 UTC (rev 4474) +++ trunk/Mail/ChangeLog 2007-01-09 10:11:01 UTC (rev 4475) @@ -1,3 +1,11 @@ +1.2.1 - [RELEASEDATE] +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Added ezcMailTools::guessContentType to resolve a bug in which the images + embeded in an html part were treated like application/octet-stream + attachments. + + 1.2 - Monday 18 December 2006 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Modified: trunk/Mail/src/composer.php =================================================================== --- trunk/Mail/src/composer.php 2007-01-09 09:12:33 UTC (rev 4474) +++ trunk/Mail/src/composer.php 2007-01-09 10:11:01 UTC (rev 4475) @@ -301,9 +301,9 @@ if ( $this->htmlText != '' ) { // recognize file:// and file:///, pick out the image, add it as a part and then..:) - preg_match_all( "/file:\/\/[^ >\'\"]+/i", $this->htmlText, $matches ); + preg_match_all( "/file:\/\/([^ >\'\"]+)/i", $this->htmlText, $matches ); // pictures/files can be added multiple times. We only need them once. - $matches = array_unique( $matches[0] ); + $matches = array_unique( $matches[1] ); $result = new ezcMailText( $this->htmlText, $this->charset ); $result->subType = "html"; @@ -322,10 +322,28 @@ { // @todo waiting for fix of the fileinfo extension // $contents = file_get_contents( $fileName ); - $filePart = new ezcMailFile( $fileName ); + $mimeType = null; + $contentType = null; + if ( ezcBaseFeatures::hasExtensionSupport( 'fileinfo' ) ) + { + // if fileinfo extension is available + $filePart = new ezcMailFile( $fileName ); + } + elseif ( ezcMailTools::guessContentType( $fileName, $contentType, $mimeType ) ) + { + // if fileinfo extension is not available try to get content/mime type + // from the file extension + $filePart = new ezcMailFile( $fileName, $contentType, $mimeType ); + } + else + { + // fallback in case fileinfo is not available and could not get content/mime + // type from file extension + $filePart = new ezcMailFile( $fileName, "application", "octet-stream" ); + } $cid = $result->addRelatedPart( $filePart ); // replace the original file reference with a reference to the cid - $this->htmlText = str_replace( $fileName, 'cid:' . $cid, $this->htmlText ); + $this->htmlText = str_replace( 'file://' . $fileName, 'cid:' . $cid, $this->htmlText ); } else { Modified: trunk/Mail/src/tools.php =================================================================== --- trunk/Mail/src/tools.php 2007-01-09 09:12:33 UTC (rev 4474) +++ trunk/Mail/src/tools.php 2007-01-09 10:11:01 UTC (rev 4475) @@ -444,5 +444,55 @@ return $reply; } + + /** + * Guesses the content and mime type by using the file extension. + * + * The content and mime types are returned through the $contentType + * and $mimeType arguments. + * For the moment only for image files. + * + * @param string $fileName + * @param string $contentType + * @param string $mimeType + */ + static public function guessContentType( $fileName, &$contentType, &$mimeType ) + { + $extension = strtolower( pathinfo( $fileName, PATHINFO_EXTENSION ) ); + switch ( $extension ) + { + case 'gif': + $contentType = 'image'; + $mimeType = 'gif'; + break; + + case 'jpg': + case 'jpe': + case 'jpeg': + $contentType = 'image'; + $mimeType = 'jpeg'; + break; + + case 'png': + $contentType = 'image'; + $mimeType = 'png'; + break; + + case 'bmp': + $contentType = 'image'; + $mimeType = 'bmp'; + break; + + case 'tif': + case 'tiff': + $contentType = 'image'; + $mimeType = 'tiff'; + break; + + default: + return false; + } + return true; + } } ?> Modified: trunk/Mail/tests/composer_test.php =================================================================== --- trunk/Mail/tests/composer_test.php 2007-01-09 09:12:33 UTC (rev 4474) +++ trunk/Mail/tests/composer_test.php 2007-01-09 10:11:01 UTC (rev 4475) @@ -266,6 +266,29 @@ } /** + * Tests a complete mail with html images and files + * http://www.apps.ietf.org/msglint.html - validator + */ + public function testMailHtmlWithImagesNoExtension() + { + $tempDir = $this->createTempDir( 'ezcMailComposerTest' ); + $fileName = $tempDir . "/fly_no_extension"; + $fileHandle = fopen( $fileName, "wb" ); + fwrite( $fileHandle, "some contents" ); + fclose( $fileHandle ); + $this->mail->from = new ezcMailAddress( '[EMAIL PROTECTED]', 'Frederik Holljen' ); + $this->mail->addTo( new ezcMailAddress( '[EMAIL PROTECTED]', 'Frederik Holljen' ) ); + $this->mail->subject = "HTML message with embeded files and images."; + $this->mail->htmlText = "<html>Some text before the simage: <img src=\"file://" + . realpath( $fileName ) . " />Here is some text after the image. Here is the <a href=\"file://" + . dirname( __FILE__ ) + . "/parts/data/fly.jpg\">file.</a></html>"; + $this->mail->addAttachment( dirname( __FILE__) . "/parts/data/fly.jpg" ); + $this->mail->build(); + // $transport = new ezcMailTransportSmtp( "smtp.ez.no" ); + } + + /** * Tests a mail with unreadable html images. */ public function testMailHtmlWithImagesUnreadable() Modified: trunk/Mail/tests/tools_test.php =================================================================== --- trunk/Mail/tests/tools_test.php 2007-01-09 09:12:33 UTC (rev 4474) +++ trunk/Mail/tests/tools_test.php 2007-01-09 10:11:01 UTC (rev 4475) @@ -254,6 +254,38 @@ $this->assertEquals( $reply->to, array( new ezcMailAddress( '[EMAIL PROTECTED]', "Reply G\xC3\xA5r", 'utf-8' ) ) ); } + public function testGuessContentType() + { + $fileNames = array( '/home/1.jpg', + '2.jpe', + '3.jpeg', + '4.gif', + '5.tif', + '6.tiff', + '7.bmp', + '8.png', + '9.xxx', + '10' + ); + $types = array( 'image/jpeg', + 'image/jpeg', + 'image/jpeg', + 'image/gif', + 'image/tiff', + 'image/tiff', + 'image/bmp', + 'image/png', + '/', + '/' ); + for ( $i = 0; $i < count( $fileNames ); $i++ ) + { + $contentType = null; + $mimeType = null; + ezcMailTools::guessContentType( $fileNames[$i], $contentType, $mimeType ); + $this->assertEquals( $types[$i], $contentType . '/' . $mimeType ); + } + } + public static function suite() { return new PHPUnit_Framework_TestSuite( "ezcMailToolsTest" ); -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components