/** * Adds the part $part to the list of multipart messages. * * This method is called automatically by ezcMailMultipartParser * each time a part is parsed. * * @param ezcMailPart $part */ public function partDone(ezcMailPart $part) { // TODO: support Content-Type: start= as specified by RFC 2387 if (!$this->part->getMainPart()) { $this->part->setMainPart($part); return; } $this->part->addRelatedPart($part); }
/** * 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; }
/** * 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; }
public function testGetMultipartRelatedWithoutMain() { $part = new ezcMailMultipartRelated(); $part->addRelatedPart(new ezcMailText('a')); $this->assertEquals(1, count($part->getRelatedParts())); }