예제 #1
0
 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);
     }
 }
예제 #2
0
 /**
  * Returns an ezcMailPart based on the HTML provided.
  *
  * This method adds local files/images to the mail itself using a
  * {@link ezcMailMultipartRelated} object.
  *
  * @throws ezcBaseFileNotFoundException
  *         if $fileName does not exists.
  * @throws ezcBaseFilePermissionProblem
  *         if $fileName could not be read.
  * @return ezcMailPart
  */
 private function generateHtmlPart()
 {
     $result = false;
     if ($this->htmlText != '') {
         $matches = array();
         if ($this->options->automaticImageInclude === true) {
             // recognize file:// and file:///, pick out the image, add it as a part and then..:)
             preg_match_all("/<img[\\s\\*\\s]src=[\\'\"]file:\\/\\/([^ >\\'\"]+)/i", $this->htmlText, $matches);
             // pictures/files can be added multiple times. We only need them once.
             $matches = array_unique($matches[1]);
         }
         $result = new ezcMailText($this->htmlText, $this->charset, $this->encoding);
         $result->subType = "html";
         if (count($matches) > 0) {
             $htmlPart = $result;
             // wrap already existing message in an alternative part
             $result = new ezcMailMultipartRelated($result);
             // create a filepart and add it to the related part
             // also store the ID for each part since we need those
             // when we replace the originals in the HTML message.
             foreach ($matches as $fileName) {
                 if (is_readable($fileName)) {
                     // @todo waiting for fix of the fileinfo extension
                     // $contents = file_get_contents( $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('file://' . $fileName, 'cid:' . $cid, $this->htmlText);
                 } else {
                     if (file_exists($fileName)) {
                         throw new ezcBaseFilePermissionException($fileName, ezcBaseFileException::READ);
                     } else {
                         throw new ezcBaseFileNotFoundException($fileName);
                     }
                     // throw
                 }
             }
             // update mail, with replaced URLs
             $htmlPart->text = $this->htmlText;
         }
     }
     return $result;
 }
 /**
  * Returns an ezcMailPart based on the HTML provided.
  *
  * This method adds local files/images to the mail itself using a
  * {@link ezcMailMultipartRelated} object.
  *
  * @throws ezcBaseFileNotFoundException
  *         if $fileName does not exists.
  * @throws ezcBaseFilePermissionProblem
  *         if $fileName could not be read.
  * @return ezcMailPart
  */
 private function generateHtmlPart()
 {
     $result = false;
     if ($this->htmlText != '') {
         $matches = array();
         if ($this->options->automaticImageInclude === true) {
             /*
                              1.7.1 regex is buggy filename are not extract correctly
                              http://issues.ez.no/IssueView.php?Id=16612&
             
                              preg_match_all( '(
                                 <img \\s+[^>]*
                                     src\\s*=\\s*
                                         (?:
                                             (?# Match quoted attribute)
                                             ([\'"])file://(?P<quoted>[^>]+)\\1
             
                                             (?# Match unquoted attribute, which may not contain spaces)
                                         |   file://(?P<unquoted>[^>\\s]+)
                                     )
                                 [^>]* >)ix', $htmlText, $matches );
             
             
                              * */
             // CJW Newsletter regex change only find all  file://ezroot/
             // so it is secure
             // recognize file://ezroot/ and pick out the image, add it as a part and then..:)
             preg_match_all("/<img[\\s\\*\\s]src=[\\'\"]file:\\/\\/ezroot\\/([^ >\\'\"]+)/i", $this->htmlText, $matches);
             // pictures/files can be added multiple times. We only need them once.
             $matches = array_unique($matches[1]);
         }
         $result = new ezcMailText($this->htmlText, $this->charset, $this->encoding);
         $result->subType = "html";
         if (count($matches) > 0) {
             $htmlPart = $result;
             // wrap already existing message in an alternative part
             $result = new ezcMailMultipartRelated($result);
             // create a filepart and add it to the related part
             // also store the ID for each part since we need those
             // when we replace the originals in the HTML message.
             foreach ($matches as $fileName) {
                 if (is_readable($fileName)) {
                     // @todo waiting for fix of the fileinfo extension
                     // $contents = file_get_contents( $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('file://ezroot/' . $fileName, 'cid:' . $cid, $this->htmlText);
                 } else {
                     if (file_exists($fileName)) {
                         throw new ezcBaseFilePermissionException($fileName, ezcBaseFileException::READ);
                     } else {
                         throw new ezcBaseFileNotFoundException($fileName);
                     }
                     // throw
                 }
             }
             // update mail, with replaced URLs
             $htmlPart->text = $this->htmlText;
         }
     }
     return $result;
 }