protected function parseAddress($addresses) { $parser = new waMailAddressParser($addresses); $data = $parser->parse(); if ($data) { return $data[0]; } return false; }
/** Parse a decoded email header into list of arrays [name => ..., email => ..., full => ...] */ public static function parseAddress($header) { $v = $header; try { $parser = new waMailAddressParser($v); $v = $parser->parse(); } catch (Exception $e) { if (preg_match('~<([^>]+)>~', $v, $m)) { $email = $m[1]; } else { if (preg_match('~(\\S+\\@\\S+)~', $v, $m)) { $email = $m[1]; } else { $email = explode(' ', $v); $email = $email[0]; } } $name = trim(preg_replace('~<?' . preg_quote($email, '~') . '>?~', '', $v)); $v = array(array('name' => $name, 'email' => $email)); } $v[0]['full'] = $header; return $v; }
public function decode($file, $full_response = false) { if (is_resource($file)) { $this->source = $file; } else { $this->source = fopen($file, 'r'); $this->options['attach_path'] = dirname($file) . '/files/'; } // start state $this->buffer = ''; $this->buffer_offset = 0; $this->is_last = false; $this->state = self::STATE_HEADER; $this->parts = array(array()); $this->attachments = array(); $this->body = array(); $this->part =& $this->parts[0]; // check end of file if (!feof($this->source)) { $part = false; while ($this->state != self::STATE_END) { if (!$part) { if ($this->is_last) { fclose($this->source); throw new waException("Письмо не было завершено, а данные в файле уже кончились."); } $this->read(); } $part = $this->parse(); if ($part && is_array($part)) { $this->decodePart($part); } } if (!$this->is_last) { if (!$this->options['headers_only']) { fclose($this->source); throw new waException("Конец письма уже достигнут. Есть какие-то данные еще."); } } } fclose($this->source); $headers = $this->parts[0]['headers']; foreach ($headers as $h => &$v) { if (is_array($v)) { $v = implode("\n", $v); } $v = $this->decodeHeader($v); if ($h == 'subject') { if (strpos($v, ' ') === false) { $v = str_replace('_', ' ', $v); } } elseif ($h == 'date') { $v = preg_replace("/[^a-z0-9:,\\.\\s\t\\+-]/i", '', $v); $v = date("Y-m-d H:i:s", strtotime($v)); } elseif ($h == 'to' || $h == 'cc') { $parser = new waMailAddressParser($v); $v = $parser->parse(); } elseif ($h == 'from') { if ($v) { try { $parser = new waMailAddressParser($v); $v = $parser->parse(); if (isset($v[0])) { $v = $v[0]; } } catch (Exception $e) { $v = array('name' => $v, 'email' => ''); } } } } unset($v); foreach (array('subject', 'from', 'to', 'cc', 'reply-to', 'date') as $h) { if (!isset($headers[$h])) { $headers[$h] = ''; } } $result = array_merge(array('headers' => $headers), $this->body); // return body if (isset($result['text/html'])) { $result['text/html'] = $this->cleanHTML($result['text/html']); if (!isset($this->body['text/plain']) || $this->body['text/html'] && !trim($this->body['text/plain'])) { $result['text/plain'] = trim(strip_tags($result['text/html'])); } } if (isset($this->body['text/plain'])) { $result['text/plain'] = trim($this->body['text/plain']); if (!isset($this->body['text/html'])) { $result['text/html'] = nl2br($result['text/plain']); } } // return attachments $result['attachments'] = $this->attachments; if ($full_response) { $result['parts'] = $this->parts; } return $result; }