'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'bmp' => 'image/bmp', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'swf' => 'application/x-shockwave-flash' ); /** * Content type of the message * @var string */ protected $_type = Zend_Mime::MULTIPART_RELATED; public function __construct($charset = 'UTF-8') { parent::__construct($charset); } public function setText($text, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE) { return $this->setBodyText($text, $charset, $encoding); } public function setHtml($html, $text = null, $imagesDir = null) { if (null !== $text) { $this->setBodyText($text); } if (null !== $imagesDir) { $this->_cleanHtmlImages(); $this->_prepareHtml($html, $imagesDir); } return $this->setBodyHtml($html); } protected function _findHtmlImages($html) { $extensions = array_keys($this->_imageTypes); $images = array(); preg_match_all('/(?:"|\')([^"\']+\.(' . implode('|', $extensions) . '))(?:"|\')/Ui', $html, $images); $htmlImages = array(); if (array_key_exists(1, $images) && is_array($images[1])) { $htmlImages = array_unique($images[1]); } return $htmlImages; } public static function cutImageBaseUrl(&$html, $baseUrl) { if (substr($baseUrl, -1) != '/') { $baseUrl .= '/'; } $dom = new Zend_Dom_Query(); $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'); $dom->setDocumentHtml($html); $results = $dom->query('img'); /** @var DOMElement $result */ foreach ($results as $result) { $src = $result->getAttribute('src'); $src = str_replace($baseUrl, '', $src); $result->setAttribute('src', $src); } $html = $results->getDocument()->saveHtml(); return true; } protected function _prepareHtml(&$html, $imagesDir) { $htmlImages = $this->_findHtmlImages($html); if (substr($imagesDir, -1) != '/') { $imagesDir .= '/'; } if (!empty($htmlImages)) { foreach ($htmlImages as $htmlImage) { if (!file_exists($imagesDir . $htmlImage)) { $this->_resizeImage(rawurldecode($htmlImage)); } if ($image = file_get_contents($imagesDir . rawurldecode($htmlImage))) { $extention = substr($htmlImage, strrpos($htmlImage, '.') + 1); $contentType = $this->_imageTypes[strtolower($extention)]; $filename = str_replace('/', '_', $htmlImage); $part = $this->attachHtmlImage($image, $filename, $contentType); if (!$part->id) { $part->id = $this->_generatePartId(); } $filename = 'cid:' . $part->id; $html = str_replace($htmlImage, $filename, $html); } } } return $this; } protected function _resizeImage($htmlImage) { $info = Qs_ImageFs::getThumbnailInfo(basename($htmlImage)); $name = $file = $width = $height = $force = $method = null; extract($info); if ($width || $height) { $name = Qs_ImageFs::resize( Qs_ImageFs::WEB_PATH . '/' . $file, array( 'width' => $width, 'height' => $height, 'force' => $force, 'method' => $method ) ); } return $name; } protected function _cleanHtmlImages() { $html = $this->getBodyHtml(true); if (!empty($html)) { $htmlImages = $this->_findHtmlImages($html); if (!empty($htmlImages)) { foreach ($htmlImages as $htmlImage) { $filename = str_replace('/', '_', $htmlImage); $this->deleteAttachmentByName($filename); } } } return $this; } // аналог HttpMimeMail::addAttachment() public function attachFile($file, $filename = null, $contentType = Zend_Mime::TYPE_OCTETSTREAM, $encoding = Zend_Mime::ENCODING_BASE64) { if (!empty($file)) { $part = $this->createAttachment( $file, $contentType, Zend_Mime::DISPOSITION_ATTACHMENT, $encoding, $filename ); $part->id = $this->_generatePartId(); return $part; } return false; } public function attachHtmlImage($file, $filename, $contentType = Zend_Mime::TYPE_OCTETSTREAM, $encoding = Zend_Mime::ENCODING_BASE64) { if (!empty($file) && !empty($filename)) { $part = $this->createAttachment( $file, $contentType, Zend_Mime::DISPOSITION_INLINE, $encoding, $filename ); $part->id = $this->_generatePartId(); return $part; } return false; } // фікс проблеми з передачею Return-Path для Sendmail public function send($transport = null) { if ($transport === null) { if (!self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) { $transport = new Zend_Mail_Transport_Sendmail(); } else { $transport = self::$_defaultTransport; } } if ($transport instanceof Zend_Mail_Transport_Sendmail) { if (empty($transport->parameters)) { $returnPath = $this->getReturnPath(); if ($returnPath) { $transport->parameters = '-f' . $returnPath; } } } return parent::send($transport); } public function deleteAttachmentByName($filename) { $newParts = array(); $parts = $this->getParts(); foreach ($parts as &$part) { if ($part->filename != $filename) { $newParts[] = $part; } else { unset($part); } } $this->setParts($newParts); return $this; } public function setFrom($email, $name = null) { if (null === $name) { $_email = $this->_filterEmail($email); $name = str_replace(array($_email, '<', '>'), '', $email); $name = $this->_filterName($name); $email = $_email; } return parent::setFrom($email, $name); } protected function _filterEmail($email) { $pattern = '/((?:(?:\"[^\"\f\n\r\t\v\b]+\")|(?:[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(?:\.[\w\!\#\$\%\&\'\*\+\-' . '\~\/\^\`\|\{\}]+)*))@(?:(?:\[(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.(?:(?:25[0-' . '5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))' . '\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9])))\])|(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:' . '[0-1]?[0-9]?[0-9]))\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.(?:(?:25[0-5])|(?:2[0' . '-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9])))|(?:(?:(?' . ':[A-Za-z0-9\-])+\.)+[A-Za-z\-]+)))/'; $matches = array(); preg_match($pattern, $email, $matches); $email = reset($matches); return parent::_filterEmail($email); } protected function _generatePartId() { return md5(uniqid(time())); } }