Exemplo n.º 1
0
 /**
  * Public constructor
  *
  * In addition to the parameters of Zend_Mail_Part::__construct() this constructor supports:
  * - file filename or file handle of a file with raw message content
  *
  * @param  string $rawMessage  full message with or without headers
  * @throws Zend_Mail_Exception
  */
 public function __construct(array $params)
 {
     if (isset($params['file'])) {
         if (!is_resource($params['file'])) {
             $params['raw'] = @file_get_contents($params['file']);
             if ($params['raw'] === false) {
                 throw new Zend_Mail_Exception('could not open file');
             }
         } else {
             $params['raw'] = stream_get_contents($params['file']);
         }
     }
     parent::__construct($params);
 }
Exemplo n.º 2
0
 /**
  * Public constructor
  *
  * In addition to the parameters of Zend_Mail_Part::__construct() this constructor supports:
  * - file  filename or file handle of a file with raw message content
  * - flags array with flags for message, keys are ignored, use constants defined in Zend_Mail_Storage
  *
  * @param  string $rawMessage  full message with or without headers
  * @throws Zend_Mail_Exception
  */
 public function __construct(array $params)
 {
     if (isset($params['file'])) {
         if (!is_resource($params['file'])) {
             $params['raw'] = @file_get_contents($params['file']);
             if ($params['raw'] === false) {
                 throw new Zend_Mail_Exception('could not open file');
             }
         } else {
             $params['raw'] = stream_get_contents($params['file']);
         }
     }
     if (!empty($params['flags'])) {
         // set key and value to the same value for easy lookup
         $this->_flags = array_combine($params['flags'], $params['flags']);
     }
     parent::__construct($params);
 }
Exemplo n.º 3
0
 /**
  * Public constructor
  *
  * In addition to the parameters of Zend_Mail_Part::__construct() this constructor supports:
  * - file  filename or file handle of a file with raw message content
  * - flags array with flags for message, keys are ignored, use constants defined in Zend_Mail_Storage
  *
  * @param  string $rawMessage  full message with or without headers
  * @throws Zend_Mail_Exception
  */
 public function __construct(array $params)
 {
     if (isset($params['file'])) {
         if (!is_resource($params['file'])) {
             $params['raw'] = @file_get_contents($params['file']);
             if ($params['raw'] === false) {
                 /**
                  * @see Zend_Mail_Exception
                  */
                 require_once PHP_LIBRARY_PATH . 'Zend/Mail/Exception.php';
                 throw new Zend_Mail_Exception('could not open file');
             }
         } else {
             $params['raw'] = stream_get_contents($params['file']);
         }
     }
     if (!empty($params['flags'])) {
         // set key and value to the same value for easy lookup
         $this->_flags = array_merge($this->_flags, array_combine($params['flags'], $params['flags']));
     }
     parent::__construct($params);
 }
Exemplo n.º 4
0
 /**
  * Public constructor
  *
  * In addition to the parameters of Zend_Mail_Part::__construct() this constructor supports:
  * - file  filename or file handle of a file with raw message content
  * - flags array with flags for message, keys are ignored, use constants defined in Zend_Mail_Storage
  *
  * @param  string $rawMessage  full message with or without headers
  * @throws Zend_Mail_Exception
  */
 public function __construct(array $params)
 {
     if (isset($params['file'])) {
         if (!is_resource($params['file'])) {
             $params['raw'] = @file_get_contents($params['file']);
             if ($params['raw'] === false) {
                 /**
                  * @see Zend_Mail_Exception
                  */
                 throw new Zend_Mail_Exception('could not open file');
             }
         } else {
             $params['raw'] = stream_get_contents($params['file']);
         }
         $params['raw'] = preg_replace("/(?<!\r)\n/", "\r\n", $params['raw']);
     }
     if (!empty($params['flags'])) {
         // set key and value to the same value for easy lookup
         $this->_flags = array_merge($this->_flags, array_combine($params['flags'], $params['flags']));
     }
     parent::__construct($params);
 }
Exemplo n.º 5
0
 function renderMail($obj, $controller, $field, Am_Grid_Field_Expandable $fieldObj)
 {
     $_body = $obj->body;
     $atRendered = null;
     $headersRendered = '';
     foreach (unserialize($obj->headers) as $headerName => $headerVal) {
         if (isset($headerVal['append'])) {
             unset($headerVal['append']);
             $headerVal = implode(',' . "\r\n" . ' ', $headerVal);
         } else {
             $headerVal = implode("\r\n", $headerVal);
         }
         $_headers[strtolower($headerName)] = $headerVal;
         if (strpos($headerVal, '=?') === 0) {
             $headerVal = mb_decode_mimeheader($headerVal);
         }
         $headersRendered .= '<strong>' . $headerName . '</strong>: <em>' . nl2br(Am_Controller::escape($headerVal)) . '</em><br />';
     }
     $part = new Zend_Mail_Part(array('headers' => $_headers, 'content' => $_body));
     $canHasAttacments = false;
     list($type) = explode(";", $part->getHeader('content-type'));
     if ($type == 'multipart/alternative') {
         $msgPart = $part->getPart(2);
     } else {
         $msgPart = $part->isMultipart() ? $part->getPart(1) : $part;
         if ($msgPart->isMultipart()) {
             $msgPart = $msgPart->getPart(2);
             //html part
         }
         $canHasAttacments = true;
     }
     list($type) = explode(";", $msgPart->getHeader('content-type'));
     $encoding = $msgPart->getHeader('content-transfer-encoding');
     $content = $msgPart->getContent();
     if ($encoding && $encoding == 'quoted-printable') {
         $content = quoted_printable_decode($content);
     } else {
         $content = base64_decode($content);
     }
     switch ($type) {
         case 'text/plain':
             $bodyRendered = nl2br(Am_Controller::escape($content));
             break;
         case 'text/html':
             $content = preg_replace('#^.*<body.*?>#mi', '', $content);
             $content = preg_replace('#</body>.*$#mi', '', $content);
             $bodyRendered = $content;
             break;
     }
     //attachments
     $atRendered = '';
     if ($canHasAttacments) {
         if ($part->isMultipart()) {
             for ($i = 2; $i <= $part->countParts(); $i++) {
                 $attPart = $part->getPart($i);
                 preg_match('/filename="(.*)"/', $attPart->{'content-disposition'}, $matches);
                 $filename = @$matches[1];
                 $atRendered .= sprintf("&ndash; %s (<em>%s</em>)", $filename, $attPart->{'content-type'}) . '<br />';
             }
         }
     }
     $attachTitle = ___('Attachments');
     return $headersRendered . '<br />' . $bodyRendered . ($atRendered ? '<br /><strong>' . $attachTitle . '</strong>:<br />' . $atRendered : '');
 }
 public function parseMessage($mail, $message, $i)
 {
     $response = array('message' => array('subject' => null, 'fromEmail' => null, 'message' => null, 'receiveDate' => null), 'attachments' => array());
     // Get the UTC timestamp
     $timezone = date_default_timezone_get();
     date_default_timezone_set('UTC');
     $receiveDate = strtotime($message->date);
     date_default_timezone_set($timezone);
     // Get from
     $from = $message->from;
     $from = str_replace(array('<', '>'), '', $from);
     $from = explode(' ', $from);
     $from = array_pop($from);
     $response['message']['fromEmail'] = $from;
     // Get the message
     $messageBody = '';
     $boundary = $message->getHeaderField('content-type', 'boundary');
     if (stristr($message->contentType, 'text/')) {
         $messageBody = $mail->getRawContent($i);
     } else {
         if (stristr($message->contentType, 'multipart/')) {
             $messageParts = new Zend_Mail_Part(array('handler' => &$mail, 'id' => $i, 'headers' => $message->getHeaders()));
             // Get the messages's contents. When fetched it removes the
             // message
             $messageParts->getContent();
             foreach ($messageParts as $partIndex => $part) {
                 $attachment = array('ticketId' => null, 'replyId' => null, 'filename' => null, 'contentType' => null, 'content' => null);
                 if ($partIndex === 1) {
                     // Decode attachment's data
                     $content = (string) $part;
                     if ($part->headerExists('content-transfer-encoding') and $part->contentTransferEncoding === 'base64') {
                         $content = base64_decode($content);
                     }
                     //-- If an email is set with html + attachment + text alternative, then zend doesn't pickup the sub boundary :(
                     $sub_boundary = preg_match('([a-zA-Z0-9]{28})', $content, $sub_boundaries);
                     if ($sub_boundary > 0) {
                         $subparts = explode('--' . $sub_boundaries[0], $content);
                         foreach ($subparts as $subpart) {
                             if (stristr($subpart, 'text/html')) {
                                 $quoted = false;
                                 if (stristr($subpart, 'quoted-printable')) {
                                     $quoted = true;
                                 }
                                 $content = explode("\n\n", $subpart);
                                 array_shift($content);
                                 $content = implode("\n\n", $content);
                                 if ($quoted) {
                                     $content = Zend_Mime_Decode::decodeQuotedPrintable($content);
                                     // Gay ass word wrapping with hmtl is bad idea.
                                     $content = str_replace(array("=\n", '=3D'), array('', '='), $content);
                                 }
                             }
                         }
                     }
                     $content = nl2br($content);
                     $messageBody = $content;
                 } else {
                     $attachment['contentType'] = $part->contentType;
                     // Grab the filename
                     $attachment['filename'] = $part->getHeaderField('content-type', 'name');
                     // Decode attachment's data
                     $content = (string) $part;
                     // TODO: Need to part before assuming it has a transfer encoding
                     if ($part->contentTransferEncoding === 'base64') {
                         $content = base64_decode($content);
                     }
                     // TODO: other encodings?
                     $attachment['content'] = $content;
                     array_push($response['attachments'], $attachment);
                 }
             }
         }
     }
     $response['message']['subject'] = (string) $message->subject;
     $response['message']['fromDate'] = (string) $message->from;
     $response['message']['message'] = $messageBody;
     $response['message']['receiveDate'] = $receiveDate;
     return $response;
 }
Exemplo n.º 7
0
 protected function _extractHeaderToParams(Zend_Mail_Part $part, $header_name, &$params, $is_array = true)
 {
     try {
         $header_content = $part->getHeader($header_name, 'string');
         $param_name = str_replace('-', '', $header_name);
         foreach (array_filter(array_map('trim', explode(',', $header_content))) as $header_content_line) {
             if (preg_match('/(.*)<(.*)>/', $header_content_line, $m)) {
                 $email = trim($m[2]);
                 $name = trim($m[1]);
             } else {
                 $email = trim($header_content_line);
                 $name = '';
             }
             if ($is_array) {
                 $params[$param_name][] = $email;
                 $params[$param_name . 'name'][] = $name;
             } else {
                 $params[$param_name] = $email;
                 $params[$param_name . 'name'] = $name;
             }
         }
     } catch (Exception $e) {
     }
 }