/** * Returns an ezcMailAddress object parsed from the address string $address. * * You can set the encoding of the name part with the $encoding parameter. * If $encoding is omitted or set to "mime" parseEmailAddress will asume that * the name part is mime encoded. * * This method does not perform validation. It will also accept slightly * malformed addresses. * * If the mail address given can not be decoded null is returned. * * Example: * <code> * ezcMailTools::parseEmailAddress( 'John Doe <*****@*****.**>' ); * </code> * * @param string $address * @param string $encoding * @return ezcMailAddress */ public static function parseEmailAddress($address, $encoding = "mime") { // we don't care about the "group" part of the address since this is not used anywhere $matches = array(); $pattern = '/<?\\"?[a-zA-Z0-9!#\\$\\%\\&\'\\*\\+\\-\\/=\\?\\^_`{\\|}~\\.]+\\"?@[a-zA-Z0-9!#\\$\\%\\&\'\\*\\+\\-\\/=\\?\\^_`{\\|}~\\.]+>?$/'; if (preg_match(trim($pattern), $address, $matches, PREG_OFFSET_CAPTURE) != 1) { return null; } $name = substr($address, 0, $matches[0][1]); // trim <> from the address and "" from the name $name = trim($name, '" '); $mail = trim($matches[0][0], '<>'); // remove any quotes found in mail addresses like "bah,"@example.com $mail = str_replace('"', '', $mail); if ($encoding == 'mime') { // the name may contain interesting character encoding. We need to convert it. $name = ezcMailTools::mimeDecode($name); } else { $name = ezcMailCharsetConverter::convertToUTF8($name, $encoding); } $address = new ezcMailAddress($mail, $name, 'utf-8'); return $address; }
/** * Returns an ezcMail corresponding to the parsed message. * You can specify an alternate class using the $class parameter, if you * extended ezcMail. * * @param string $class Class to instanciate instead of ezcMail. * @return ezcMail */ public function finish($class = "ezcMail") { $mail = new $class(); $mail->setHeaders($this->headers->getCaseSensitiveArray()); ezcMailPartParser::parsePartHeaders($this->headers, $mail); // from if (isset($this->headers['From'])) { $mail->from = ezcMailTools::parseEmailAddress($this->headers['From']); } // to if (isset($this->headers['To'])) { $mail->to = ezcMailTools::parseEmailAddresses($this->headers['To']); } // cc if (isset($this->headers['Cc'])) { $mail->cc = ezcMailTools::parseEmailAddresses($this->headers['Cc']); } // bcc if (isset($this->headers['Bcc'])) { $mail->bcc = ezcMailTools::parseEmailAddresses($this->headers['Bcc']); } // subject if (isset($this->headers['Subject'])) { $mail->subject = ezcMailTools::mimeDecode($this->headers['Subject']); $mail->subjectCharset = 'utf-8'; } // message ID if (isset($this->headers['Message-Id'])) { $mail->messageID = $this->headers['Message-Id']; } // Return-Path if (isset($this->headers['Return-Path'])) { $mail->returnPath = ezcMailTools::parseEmailAddress($this->headers['Return-Path']); } if ($this->bodyParser !== null) { $mail->body = $this->bodyParser->finish(); } return $mail; }
/** * Sorts message numbers array $messages by the specified $sortCriteria. * * This method supports unique IDs instead of message numbers. See * {@link ezcMailImapTransportOptions} for how to enable unique IDs * referencing. * * $messages is an array of message numbers, for example: * <code> * array( 1, 2, 4 ); * </code> * * $sortCriteria is an email header like: Subject, To, From, Date, Sender. * * The sorting is done with the php function natcasesort(). * * Before calling this method, a connection to the IMAP server must be * established and a user must be authenticated successfully, and a mailbox * must be selected. * * @throws ezcMailTransportException * if a mailbox is not selected * or if the server sent a negative response * or if the array $messages is empty * @param array(int) $messages * @param string $sortCriteria * @param bool $reverse * @return array(string) */ protected function sort($messages, $sortCriteria, $reverse = false) { $uid = $this->options->uidReferencing ? self::UID : self::NO_UID; if ($this->state != self::STATE_SELECTED && $this->state != self::STATE_SELECTED_READONLY) { throw new ezcMailTransportException("Can't call sort() on the IMAP transport when a mailbox is not selected."); } $result = array(); $query = ucfirst(strtolower($sortCriteria)); $messageNumbers = implode(',', $messages); $tag = $this->getNextTag(); $this->connection->sendData("{$tag} {$uid}FETCH {$messageNumbers} (BODY.PEEK[HEADER.FIELDS ({$query})])"); $response = trim($this->connection->getLine()); while (strpos($response, $tag) === false) { if (strpos($response, ' FETCH (') !== false) { if ($this->options->uidReferencing) { preg_match('/^\\* [0-9]+ FETCH \\(UID ([0-9]+)/', $response, $matches); } else { preg_match('/^\\* ([0-9]+) FETCH/', $response, $matches); } $messageNumber = $matches[1]; } if (strpos($response, $query) !== false) { $strippedResponse = trim(trim(str_replace("{$query}: ", '', $response)), '"'); switch ($query) { case 'Date': $strippedResponse = strtotime($strippedResponse); break; case 'Subject': case 'From': case 'Sender': case 'To': $strippedResponse = ezcMailTools::mimeDecode($strippedResponse); break; default: break; } $result[$messageNumber] = $strippedResponse; } // in case the mail doesn't have the $sortCriteria header (like junk mail missing Subject header) if (strpos($response, ')') !== false && !isset($result[$messageNumber])) { $result[$messageNumber] = ''; } $response = trim($this->connection->getLine()); } if ($this->responseType($response) != self::RESPONSE_OK) { throw new ezcMailTransportException("The IMAP server could not sort the messages: {$response}."); } if ($reverse === true) { natcasesort($result); $result = array_reverse($result, true); } else { natcasesort($result); } return $result; }
/** * Returns the a ezcMailContentDispositionHeader for the parsed $header. * * If $cd is provided this object will be used to fill in the blanks. This function * will not clear out any old values in the object. * * @param string $header * @param ezcMailContentDispositionHeader $cd * @return ezcMailContentDispositionHeader */ public static function parseContentDisposition($header, ezcMailContentDispositionHeader $cd = null) { if ($cd === null) { $cd = new ezcMailContentDispositionHeader(); } $parsedHeader = self::parseHeader($header); $cd->disposition = $parsedHeader[0]; if (isset($parsedHeader[1])) { foreach ($parsedHeader[1] as $paramName => $data) { switch ($paramName) { case 'filename': $cd->fileName = $data['value']; $cd->displayFileName = trim($data['value'], '"'); if (isset($data['charset'])) { $cd->fileNameCharSet = $data['charset']; $cd->displayFileName = ezcMailCharsetConverter::convertToUTF8Iconv($cd->displayFileName, $cd->fileNameCharSet); } else { if (preg_match('@^=\\?[^?]+\\?[QqBb]\\?@', $cd->displayFileName)) { $cd->displayFileName = ezcMailTools::mimeDecode($cd->displayFileName); } } if (isset($data['language'])) { $cd->fileNameLanguage = $data['language']; } break; case 'creation-date': $cd->creationDate = $data['value']; break; case 'modification-date': $cd->modificationDate = $data['value']; break; case 'read-date': $cd->readDate = $data['value']; break; case 'size': $cd->size = $data['value']; break; default: $cd->additionalParameters[$paramName] = $data['value']; if (isset($data['charset'])) { $cd->additionalParametersMetaData[$paramName]['charSet'] = $data['charset']; } if (isset($data['language'])) { $cd->additionalParametersMetaData[$paramName]['language'] = $data['language']; } break; } } } return $cd; }