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 getHeader($name, $format = null) { $result = parent::getHeader($name, $format); if ('array' !== $format && function_exists('mb_decode_mimeheader')) { $result = mb_decode_mimeheader($result); } if ('from' === strtolower($name) || 'to' === strtolower($name)) { $result = $this->extractAddrSpec($result); } return $result; }
/** * メール読み取り */ 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 testMultipleHeader() { $raw = file_get_contents($this->_file); $raw = "sUBject: test\nSubJect: test2\n" . $raw; $message = new Zend_Mail_Message(array('raw' => $raw)); $this->assertEquals($message->getHeader('subject', 'string'), "test\r\ntest2\r\nmultipart"); $this->assertEquals($message->getHeader('subject'), array('test', 'test2', 'multipart')); }
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); } } } }
/** * set headers * * @param Zend_Mail_Message $_zmm * @return Zend_Mail Provides fluent interface */ public function setHeadersFromZMM(Zend_Mail_Message $_zmm) { 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 = self::parseAdresslist($value); foreach ($addresses as $address) { $this->addBcc($address['address'], $address['name']); } break; case 'cc': $addresses = self::parseAdresslist($value); foreach ($addresses as $address) { $this->addCc($address['address'], $address['name']); } break; case 'date': try { $this->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); } $this->setDate(); } break; case 'from': $addresses = self::parseAdresslist($value); foreach ($addresses as $address) { $this->setFrom($address['address'], $address['name']); } break; case 'message-id': $this->setMessageId($value); break; case 'return-path': $this->setReturnPath($value); break; case 'subject': $this->setSubject($value); break; case 'to': $addresses = self::parseAdresslist($value); foreach ($addresses as $address) { $this->addTo($address['address'], $address['name']); } break; default: $this->addHeader($header, $value); break; } } } return $this; }
/** * * @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 testManualIterator() { $message = new Zend_Mail_Message(array('file' => $this->_file)); $this->assertTrue($message->valid()); $this->assertEquals($message->getChildren(), $message->current()); $this->assertEquals($message->key(), 1); $message->next(); $this->assertTrue($message->valid()); $this->assertEquals($message->getChildren(), $message->current()); $this->assertEquals($message->key(), 2); $message->next(); $this->assertFalse($message->valid()); $message->rewind(); $this->assertTrue($message->valid()); $this->assertEquals($message->getChildren(), $message->current()); $this->assertEquals($message->key(), 1); }
public function testMessageFlagsAreSet() { $origFlags = array('foo' => 'bar', 'baz' => 'bat'); $message = new Zend_Mail_Message(array('flags' => $origFlags)); $messageFlags = $message->getFlags(); $this->assertTrue($message->hasFlag('bar'), var_export($messageFlags, 1)); $this->assertTrue($message->hasFlag('bat'), var_export($messageFlags, 1)); $this->assertEquals(array('bar' => 'bar', 'bat' => 'bat'), $messageFlags); }
/** * @group ZF-3745 */ public function testMessageAcceptsPartClassOverrideViaSetter() { $message = new Zend_Mail_Message(array('file' => $this->_file)); $message->setPartClass('ZF3745_Mail_Part'); $this->assertEquals('ZF3745_Mail_Part', $message->getPartClass()); // Ensure message parts use the specified part class $this->assertGreaterThan(0, count($message)); foreach ($message as $part) { $this->assertEquals('ZF3745_Mail_Part', get_class($part)); } }
/** * send email * * @param resource $inputStream * @param boolean $saveInSent * @throws Syncroton_Exception */ public function sendEmail($inputStream, $saveInSent) { $defaultAccountId = Tinebase_Core::getPreference('Expressomail')->{Expressomail_Preference::DEFAULTACCOUNT}; try { $account = Expressomail_Controller_Account::getInstance()->get($defaultAccountId); } catch (Tinebase_Exception_NotFound $ten) { if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " no email account configured"); } throw new Syncroton_Exception('no email account configured'); } if (empty(Tinebase_Core::getUser()->accountEmailAddress)) { throw new Syncroton_Exception('no email address set for current user'); } if (!is_resource($inputStream)) { $stream = fopen("php://temp", 'r+'); fwrite($stream, $inputStream); $inputStream = $stream; rewind($inputStream); } if ($this->_debugEmail == true) { $debugStream = fopen("php://temp", 'r+'); stream_copy_to_stream($inputStream, $debugStream); rewind($debugStream); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " email to send:" . stream_get_contents($debugStream)); } //replace original stream wirh debug stream, as php://input can't be rewinded $inputStream = $debugStream; rewind($inputStream); } $incomingMessage = new Zend_Mail_Message(array('file' => $inputStream)); $subject = $incomingMessage->headerExists('subject') ? $incomingMessage->getHeader('subject') : null; if (Tinebase_Mail::isiMIPMail($incomingMessage)) { if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Do not send iMIP message with subject "' . $subject . '". The server should handle those.'); } } else { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Send Message with subject " . $subject . " (saveInSent: " . $saveInSent . ")"); } $mail = Tinebase_Mail::createFromZMM($incomingMessage); Expressomail_Controller_Message_Send::getInstance()->sendZendMail($account, $mail, (bool) $saveInSent); } }
public function testCaseInsensitiveMultipart() { $message = new Zend_Mail_Message(array('raw' => "coNTent-TYpe: muLTIpaRT/x-empty\r\n\r\n")); $this->assertTrue($message->isMultipart()); }
/** * 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; }