/** * Get the emails from a given server * * @param string $server * @param string $user * @param string $pass * @param boolean $delete whether to delete the emails after * retrieval */ public function readEmailFrom($server, $user, $pass, $delete = true, $secure = false) { $parts = split(":", $server); $port = "110"; if (count($parts) == 2) { $port = $parts[1]; } if ($port == "995") { $secure = true; } $server = $parts[0]; // Connect to mail server include_once dirname(__FILE__) . '/lib/pop3.phpclasses.php'; $pop3 = new pop3_class(); $pop3->hostname = $server; $pop3->port = $port; // $pop3->join_continuation_header_lines=1; if ($secure) { $pop3->tls = 1; } if (($error = $pop3->open()) != "") { throw new Exception($error); } if (($error = $pop3->login($user, $pass)) != "") { throw new Exception($error); } $messageInfo = $pop3->ListMessages("", 0); $pop3->Statistics($messages, $size); $count = $messages; $emails = array(); $this->log->debug("Retrieved {$count} new emails"); for ($i = 1; $i <= $count; $i++) { // If result at $i - 1 > 50000, just delete, it's too big to process $size = $messageInfo[$i]; if ($size > za()->getConfig('email_max_size', 100000)) { // skip for now, NEED TO SEND A BOUNCEBACK! $pop3->DeleteMessage($i); $this->log->err("Deleted email of size {$size}"); continue; } if (($error = $pop3->RetrieveMessage($i, $headers, $body, -1)) != "") { $this->log->err("Failed retrieving message: " . $error); continue; } $email = implode("\r\n", $headers); $email .= "\r\n\r\n" . implode("\r\n", $body); $email .= "\r\n."; $decoder = new Mail_mimeDecode($email); $email = $decoder->decode(array('include_bodies' => true)); if ($delete) { // $this->popService->delete_mail($i); if (($error = $pop3->DeleteMessage($i)) != "") { $this->log->err("Failed deleting message {$i}: " . $error); continue; } $this->log->debug("Deleted message {$i}"); } else { $this->log->debug("No messages being deleted"); } if ($email !== false) { if ($this->isAutoReply($email)) { // log and quit $from = ifset($email->headers, 'from', "unknown@email"); $this->log->warn("AutoReply email from " . $from . " has been ignored"); } else { $emails[] = $email; } } else { $this->log->err("Failed decoding email {$i}"); } } $pop3->Close(); return $emails; }