/** * create Tinebase_Mail from Zend_Mail_Message * * @param Zend_Mail_Message $_zmm * @param string $_replyBody * @return Tinebase_Mail */ public static function createFromZMM(Zend_Mail_Message $_zmm, $_replyBody = null) { $contentStream = fopen("php://temp", 'r+'); fputs($contentStream, $_zmm->getContent()); rewind($contentStream); $mp = new Zend_Mime_Part($contentStream); self::_getMetaDataFromZMM($_zmm, $mp); // append old body when no multipart/mixed if ($_replyBody !== null && $_zmm->headerExists('content-transfer-encoding')) { $mp = self::_appendReplyBody($mp, $_replyBody); } else { $mp->decodeContent(); if ($_zmm->headerExists('content-transfer-encoding')) { switch ($_zmm->getHeader('content-transfer-encoding')) { case Zend_Mime::ENCODING_BASE64: // BASE64 encode has a bug that swallows the last char(s) $bodyEncoding = Zend_Mime::ENCODING_7BIT; break; default: $bodyEncoding = $_zmm->getHeader('content-transfer-encoding'); } } else { $bodyEncoding = Zend_Mime::ENCODING_7BIT; } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Using encoding: ' . $bodyEncoding); } $mp->encoding = $bodyEncoding; } $result = new Tinebase_Mail('utf-8'); $result->setBodyText($mp); $result->setHeadersFromZMM($_zmm); return $result; }
public function __construct(Zend_Mail_Message $message) { $controller = $message->getHeader('to', 'string'); $controller = $this->formatControllerName($controller); $this->setParam($this->getControllerKey(), $controller); $action = $message->getHeader('subject', 'string'); $action = $this->formatActionName($action); $this->setParam($this->getActionKey(), $action); $params = $message->getContent(); $params = $this->formatParams($params); foreach ($params as $key => $value) { $this->setParam($key, $value); } }
public function getImages() { $images = array(); $allowTypes = array('image/jpeg', 'image/png', 'image/gif'); if (!$this->isMultiPart()) { $type = strtok($this->getPart(0)->contentType, ';'); if (in_array($type, $allowTypes)) { $image[] = array('tmp_name' => self::saveBase64ImageInTempDir(parent::getContent()), 'type' => $type); } return $images; } $current = $this->key(); $this->rewind(); foreach ($this as $part) { $tok = strtok($part->contentType, ';'); if (in_array($tok, $allowTypes)) { $images[] = array('tmp_name' => self::saveBase64ImageInTempDir($part->getContent()), 'type' => $tok); } } $this->_iterationPos = $current; return $images; }
/** * メール読み取り */ public static function readMail() { // 標準入力から読み取り $message = new Zend_Mail_Message(array('file' => 'php://stdin')); $obj = array(); // 差出人 $obj['from'] = self::getMailAddress($message->from); // 宛先 $obj['to'] = self::getMailAddress($message->to); // 件名 if ($message->getHeader('subject')) { $obj['subject'] = $message->subject; } else { $obj['subject'] = ''; } // 本文 $obj['body'] = $message->getContent(); // 不正なメールアドレスなので無視 if (preg_match("/^MAILER\\-DAEMON\\@/i", $obj['from']) || preg_match("/^postmaster\\@/i", $obj['from']) || $obj['from'] === '*****@*****.**' || strlen($obj['from']) === 0 || !preg_match("/.+\\@[a-zA-Z0-9][a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]+\$/", $obj['from'])) { exit; } // 添付ファイル処理 if ($message->isMultipart()) { // 添付ファイル数 $obj['files'] = $message->countParts() - 1; // 添付ファイル for ($i = 2; $i <= $message->countParts(); $i++) { $obj['files_type_' . ($i - 2)] = $message->getPart($i)->getHeader('content-type'); $obj['files_body_' . ($i - 2)] = $message->getPart($i)->getContent(); } } else { // 添付ファイル数 $obj['files'] = 0; } return $obj; }
public function getContent() { // If this message is multi-part-mail, // opMailMessage retrives first text/plain part as a content if ($this->isMultiPart()) { if ($this->firstTextPos) { return $this->getPart($this->firstTextPos)->getContent(); } $content = ''; $current = $this->key(); $this->rewind(); foreach ($this as $part) { $tok = strtok($part->contentType, ';'); if ('text/plain' === $tok) { $this->firstTextPos = $this->key(); $content = mb_convert_encoding($this->current()->getContent(), 'UTF-8', 'JIS'); break; } } $this->_iterationPos = $current; return $content; } return mb_convert_encoding(parent::getContent(), 'UTF-8', 'JIS'); }
public function testNoHeaderMessage() { $message = new Zend_Mail_Message(array('file' => __FILE__)); $this->assertEquals(substr($message->getContent(), 0, 5), '<?php'); $raw = file_get_contents(__FILE__); $raw = "\t" . $raw; $message = new Zend_Mail_Message(array('raw' => $raw)); $this->assertEquals(substr($message->getContent(), 0, 6), "\t<?php"); }
public function __construct($mail, Zend_Mail_Message $msg, $count) { try { $this->subject = $msg->subject; } catch (Exception $e) { } $this->oMail = $mail; $this->oMailMessage = $msg; $this->oMailCnt = $count; if (strpos($msg->from, ' ') === false) { $this->sender = str_replace(array('>', '<'), '', $msg->from); } else { $this->sender = substr($msg->from, strpos($msg->from, '<') + 1, strpos($msg->from, '>') - strpos($msg->from, '<') - 1); } try { if (strpos($msg->contentType, 'text/plain') !== false) { $this->body = quoted_printable_decode($msg->getContent()); } else { $foundPart = null; foreach (new RecursiveIteratorIterator($msg) as $part) { try { if (strtok($part->contentType, ';') == 'text/plain') { $foundPart = $part; break; } } catch (Zend_Mail_Exception $e) { // ignore } } if ($foundPart) { $this->body = quoted_printable_decode($foundPart); } } } catch (Exception $e) { } // find the hash $r = preg_match(MailImporter::getMessageBodyHashRegularExpression(), $this->body, $matches); if ($r) { $this->validationHash = $matches[1]; if ($this->validationHash != '') { $db = Loader::db(); $r = $db->GetOne('select data from MailValidationHashes where mHash = ?', array($this->validationHash)); if ($r) { $this->dataObject = unserialize($r); } } } }
/** * * @param array $source * @param resource | string $inputStream * @param string $flag * @throws Zend_Mail_Protocol_Exception */ public function parseAndSendMessage($source, $inputStream, $flag = NULL) { $originalMessage = $this->getCompleteMessage($source['itemId'], null, false); $user = Tinebase_Core::getUser(); if (!is_resource($inputStream)) { $stream = fopen("php://temp", 'r+'); fwrite($stream, $inputStream); $inputStream = $stream; rewind($inputStream); } $incomingMessage = new Zend_Mail_Message(array('file' => $inputStream)); $headers = $incomingMessage->getHeaders(); $body = $headers['content-transfer-encoding'] == 'base64' ? base64_decode($incomingMessage->getContent()) : $incomingMessage->getContent(); $isTextPlain = strpos($headers['content-type'], 'text/plain'); $bodyLines = preg_split('/\\r\\n|\\r|\\n/', $body); $body = ''; if ($isTextPlain !== false) { foreach ($bodyLines as &$line) { $body .= htmlentities($line) . '<br>'; } } else { foreach ($bodyLines as &$line) { $body .= $line . '<br>'; } } $body = '<div>' . $body . '</div>'; $bodyOrigin = $originalMessage['body']; preg_match("/<body[^>]*>(.*?)<\\/body>/is", $bodyOrigin, $matches); $bodyOrigin = count($matches) > 1 ? $matches[1] : $bodyOrigin; $body .= '<div>' . $bodyOrigin . '</div>'; $attachments = array(); foreach ($originalMessage['attachments'] as &$att) { try { $att['name'] = $att['filename']; $att['type'] = $att['content-type']; } catch (Exception $e) { } array_push($attachments, $att); } $recordData = array(); $recordData['note'] = ''; $recordData['content_type'] = 'text/html'; $recordData['account_id'] = $originalMessage->account_id; $recordData['to'] = is_array($headers['to']) ? $headers['to'] : array($headers['to']); $recordData['cc'] = array(); $recordData['bcc'] = array(); $recordData['subject'] = $headers['subject']; $recordData['body'] = $body; //$recordData['flags'] = array_merge($incomingMessage->getFlags(), $originalMessage['flags']); $recordData['flags'] = $flag != NULL ? $flag : ''; $recordData['original_id'] = $source['itemId']; $recordData['embedded_images'] = array(); $recordData['attachments'] = $attachments; $recordData['from_email'] = $user->accountEmailAddress; $recordData['from_name'] = $user->accountFullName; $recordData['customfields'] = array(); $message = new Expressomail_Model_Message(); $message->setFromJsonInUsersTimezone($recordData); try { Expressomail_Controller_Message_Send::getInstance()->sendMessage($message); } catch (Zend_Mail_Protocol_Exception $zmpe) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not send message: ' . $zmpe->getMessage()); throw $zmpe; } }
public function testLateFetch() { $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX')); $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5)); $this->assertEquals($message->countParts(), 2); $this->assertEquals($message->countParts(), 2); $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5)); $this->assertEquals($message->subject, 'multipart'); $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5)); $this->assertTrue(strpos($message->getContent(), 'multipart message') === 0); }
/** * create Felamimail message from Zend_Mail_Message * * @param Zend_Mail_Message $_zendMailMessage * @return Felamimail_Model_Message */ public static function createMessageFromZendMailMessage(Zend_Mail_Message $_zendMailMessage) { $message = new Felamimail_Model_Message(); foreach ($_zendMailMessage->getHeaders() as $headerName => $headerValue) { switch ($headerName) { case 'subject': $message->{$headerName} = $headerValue; break; case 'from': // do nothing break; case 'to': case 'bcc': case 'cc': $receipients = array(); $addresses = Tinebase_Mail::parseAdresslist($headerValue); foreach ($addresses as $address) { $receipients[] = $address['address']; } $message->{$headerName} = $receipients; break; } } $contentType = $_zendMailMessage->getHeaderField('content-type', 0); $message->content_type = $contentType; // @todo convert to utf-8 if needed $charset = $_zendMailMessage->getHeaderField('content-type', 'charset'); $encoding = $_zendMailMessage->getHeaderField('content-transfer-encoding'); switch ($encoding) { case Zend_Mime::ENCODING_QUOTEDPRINTABLE: $message->body = quoted_printable_decode($_zendMailMessage->getContent()); break; case Zend_Mime::ENCODING_BASE64: $message->body = base64_decode($_zendMailMessage->getContent()); break; default: $message->body = $_zendMailMessage->getContent(); break; } return $message; }
/** * create Tinebase_Mail from Zend_Mail_Message * * @param Zend_Mail_Message $_zmm * @param string $_replyBody * @return Tinebase_Mail */ public static function createFromZMM(Zend_Mail_Message $_zmm, $_replyBody = null) { $contentStream = fopen("php://temp", 'r+'); if (preg_match('/application\\/(x\\-){0,1}pkcs7-mime/i', $_zmm->getHeader('content-type')) > 0) { $mp = new Zend_Mime_Part($_zmm->getContent()); } else { fputs($contentStream, $_zmm->getContent()); rewind($contentStream); $mp = new Zend_Mime_Part($contentStream); } if ($_zmm->headerExists('content-transfer-encoding')) { $mp->encoding = $_zmm->getHeader('content-transfer-encoding'); $mp->decodeContent(); } else { $mp->encoding = Zend_Mime::ENCODING_7BIT; } // append old body when no multipart/mixed if ($_replyBody !== null && $_zmm->headerExists('content-transfer-encoding')) { $mp = self::_appendReplyBody($mp, $_replyBody); $mp->encoding = $_zmm->getHeader('content-transfer-encoding'); } if ($_zmm->headerExists('content-type')) { $contentTypeHeader = Zend_Mime_Decode::splitHeaderField($_zmm->getHeader('content-type')); if ($mp->type = strtolower($contentTypeHeader[0]) === 'application/pkcs7-mime') { $mp->type = $_zmm->getHeader('content-type'); } else { $mp->type = $contentTypeHeader[0]; } if (isset($contentTypeHeader['boundary'])) { $mp->boundary = $contentTypeHeader['boundary']; } if (isset($contentTypeHeader['charset'])) { $mp->charset = $contentTypeHeader['charset']; } } else { $mp->type = Zend_Mime::TYPE_TEXT; } $result = new Expressomail_Mail('utf-8'); $result->setBodyText($mp); foreach ($_zmm->getHeaders() as $header => $values) { foreach ((array) $values as $value) { switch ($header) { case 'content-transfer-encoding': // these are implicitly set by Zend_Mail_Transport_Abstract::_getHeaders() // these are implicitly set by Zend_Mail_Transport_Abstract::_getHeaders() case 'content-type': case 'mime-version': // do nothing break; case 'bcc': $addresses = Expressomail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->addBcc($address['address'], $address['name']); } break; case 'cc': $addresses = Expressomail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->addCc($address['address'], $address['name']); } break; case 'date': try { $result->setDate($value); } catch (Zend_Mail_Exception $zme) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . " Could not set date: " . $value); } if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . " " . $zme); } $result->setDate(); } break; case 'from': $addresses = Expressomail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->setFrom($address['address'], $address['name']); } break; case 'message-id': $result->setMessageId($value); break; case 'return-path': $result->setReturnPath($value); break; case 'subject': $result->setSubject($value); break; case 'to': $addresses = Expressomail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->addTo($address['address'], $address['name']); } break; default: $result->addHeader($header, $value); break; } } } return $result; }
/** * Статический метод производящий декодировку письма * * @param string $input полный оригинальный текст email с заголовками * @return array|false, в случае неверного формата входящих данных (отсутствует заголовок или часть заголовка) */ public static function getEmailData($input) { // подключить ZF autoloader ProjectConfiguration::registerZend(); $message = new Zend_Mail_Message(array('raw' => $input)); $headers = $message->getHeaders(); if (!count($headers)) { return false; } if (!isset($headers['subject']) || !isset($headers['from']) || !isset($headers['to'])) { return false; } // выдергиваем текстовое тело письма, если можем $body = null; if ($message->isMultipart()) { foreach (new RecursiveIteratorIterator($message) as $part) { try { if (strtok($part->contentType, ';') == 'text/plain') { $body = trim($part); $partHeaders = $part->getHeaders(); break; } } catch (Zend_Mail_Exception $e) { } } // в письме нет текстового варианта // FIXME как-то по человечески обрабатывать if (!$body) { throw new Exception(); } } else { $body = trim($message->getContent()); $partHeaders = $message->getHeaders(); } if (isset($partHeaders['content-transfer-encoding'])) { switch ($partHeaders['content-transfer-encoding']) { // FIXME хз нужно ли нам оно, на локалке не стоит imap_* // м.б. есть альтернативные варианты /* case '7bit': break; case '8bit': $body = quoted_printable_decode(imap_8bit($body)); break; case 'binary': $body = imap_base64(imap_binary($body)); break; case 'base64': $body = imap_base64($body); break; */ case 'quoted-printable': $body = quoted_printable_decode($body); break; case 'base64': $body = base64_decode($body); break; } } if (isset($partHeaders['content-type']) && preg_match("/^(?:.+?);\\scharset=(.+)\$/", $partHeaders['content-type'], $matches)) { $charset = trim($matches['1']); if (!preg_match("/^(utf-8|utf8)\$/i", $charset)) { $body = iconv($charset, "UTF-8//IGNORE", $body); } } $data = array('from' => self::_cleanEmail($headers['from']), 'to' => self::_cleanEmail($headers['to']), 'subject' => $headers['subject'], 'body' => $body); return $data; }
/** * @param Zend_Mail_Message $message * * @return array('filename' => $filename, 'content' => $content) | false */ protected function _getMessageAttachment($message) { $data = array(); // Get first flat part if ($message->isMultipart()) { $parts = $message; foreach (new RecursiveIteratorIterator($parts) as $part) { $attach = $this->_getMessageAttachment($part); if ($attach) { $data[] = $attach; } } } else { $headers = $message->getHeaders(); $isAttachment = null; foreach ($headers as $value) { if (is_array($value)) { $value = implode(";", $value); } if ($isAttachment = preg_match('/(name|filename)="{0,1}([^;\\"]*)"{0,1}/si', $value, $matches)) { break; } } if ($isAttachment) { $filename = $matches[2]; $encodedContent = $message->getContent(); // Decoding transfer-encoding switch ($transferEncoding = @$headers['content-transfer-encoding']) { case Zend_Mime::ENCODING_QUOTEDPRINTABLE: $content = quoted_printable_decode($encodedContent); break; case Zend_Mime::ENCODING_BASE64: $content = base64_decode($encodedContent); break; default: $content = $encodedContent; } $filename = iconv_mime_decode($filename, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8'); return array('filename' => $filename, 'content' => $content); } return false; } return $data; }