function JB_monitor_mail_box()
{
    $pop3 = new pop3_class();
    $pop3->hostname = MON_EMAIL_POP_SERVER;
    /* POP 3 server host name              */
    $pop3->port = MON_POP3_PORT;
    /* POP 3 server host port              */
    $user = MON_EMAIL_POP_USER;
    /* Authentication user name            */
    $password = MON_EMAIL_POP_PASS;
    /* Authentication password             */
    $pop3->realm = "";
    /* Authentication realm or domain      */
    $pop3->workstation = "";
    /* Workstation for NTLM authentication */
    $apop = 0;
    /* Use APOP authentication             */
    $pop3->authentication_mechanism = "USER";
    /* SASL authentication mechanism       */
    $pop3->debug = 0;
    /* Output debug information            */
    $pop3->html_debug = 0;
    /* Debug information is in HTML        */
    if ($_REQUEST['scan'] != '') {
        echo "opening Pop Connection";
    }
    if (($error = $pop3->Open()) == "") {
        if (($error = $pop3->Login($user, $password, $apop)) == "") {
            if (($error = $pop3->Statistics($messages, $size)) == "") {
                if ($_REQUEST['scan'] != '') {
                    echo "<PRE>There are {$messages} messages in the mail box with a total of {$size} bytes.</PRE>\n";
                    echo "<h3>Pop3 connection was successful.</h3>";
                }
                $result = $pop3->ListMessages("", 0);
                if (GetType($result) == "array") {
                    $result = $pop3->ListMessages('', 1);
                    // list all, unique
                    if (is_array($result)) {
                        for (Reset($result), $message = 0; $message < count($result); Next($result), $message++) {
                            if (($error = $pop3->RetrieveMessage(key($result), $headers, $body, -1)) == "") {
                                $head_txt = '';
                                $body_txt = '';
                                for ($line = 0; $line < count($headers); $line++) {
                                    $head_txt .= $headers[$line] . "\n";
                                }
                                preg_match('#Delivery-date: (.+)?\\n#i', $head_txt, $m);
                                $ts = strtotime($m[1]);
                                if (intval(MON_DEL_DAYS) > 0) {
                                    if (time() - $ts > 60 * 60 * 24 * MON_DEL_DAYS) {
                                        // more than 30 days
                                        if (($error = $pop3->DeleteMessage(key($result))) == "") {
                                            continue;
                                        }
                                    }
                                }
                                for ($line = 0; $line < count($body); $line++) {
                                    $body_txt .= $body[$line] . "\n";
                                }
                                $from_name = JB_mon_extract_from_name($head_txt);
                                if ($failed_rec = JB_mon_extract_recipient($head_txt . $body_txt)) {
                                    if (JB_match_mon_from_pattern($from_name) && JB_match_mon_body_pattern($body_txt)) {
                                        if (JB_mon_match_user_email($failed_rec)) {
                                            $user_id = JB_mon_unsubscribe_user($failed_rec);
                                            if (($error = $pop3->DeleteMessage(key($result))) == "") {
                                                JB_mon_append_log($failed_rec, 'C');
                                            }
                                        }
                                        if (JB_mon_match_employer_email($failed_rec)) {
                                            $user_id = JB_mon_unsubscribe_employer($failed_rec);
                                            if (($error = $pop3->DeleteMessage(key($result))) == "") {
                                                //echo "dlete ".key($result)."<br>";
                                                JB_mon_append_log($failed_rec, 'E');
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if ($error == "" && ($error = $pop3->Close()) == "") {
                            //echo "<PRE>Disconnected from the POP3 server &quot;".$pop3->hostname."&quot;.</PRE>\n";
                        }
                    } else {
                        $error = $result;
                    }
                } else {
                    $error = $result;
                }
            }
        }
    }
    // proces mail_monitor_log
    $now = gmdate("Y-m-d H:i:s");
    if (intval(MON_LOG_DAYS) > 0) {
        $sql = "DELETE FROM mail_monitor_log WHERE DATE_SUB('{$now}', INTERVAL " . MON_LOG_DAYS . " DAY) >= `date` ";
        JB_mysql_query($sql);
    }
}
Пример #2
0
 if (($error = $pop3->Login($user, $password, $apop)) == "") {
     echo "<PRE>User &quot;{$user}&quot; logged in.</PRE>\n";
     if (($error = $pop3->Statistics($messages, $size)) == "") {
         echo "<PRE>There are {$messages} messages in the mail box with a total of {$size} bytes.</PRE>\n";
         $result = $pop3->ListMessages("", 0);
         if (GetType($result) == "array") {
             for (Reset($result), $message = 0; $message < count($result); Next($result), $message++) {
                 echo "<PRE>Message ", Key($result), " - ", $result[Key($result)], " bytes.</PRE>\n";
             }
             $result = $pop3->ListMessages("", 1);
             if (GetType($result) == "array") {
                 for (Reset($result), $message = 0; $message < count($result); Next($result), $message++) {
                     echo "<PRE>Message ", Key($result), ", Unique ID - \"", $result[Key($result)], "\"</PRE>\n";
                 }
                 if ($messages > 0) {
                     if (($error = $pop3->RetrieveMessage(1, $headers, $body, 2)) == "") {
                         echo "<PRE>Message 1:\n---Message headers starts below---</PRE>\n";
                         for ($line = 0; $line < count($headers); $line++) {
                             echo "<PRE>", HtmlSpecialChars($headers[$line]), "</PRE>\n";
                         }
                         echo "<PRE>---Message headers ends above---\n---Message body starts below---</PRE>\n";
                         for ($line = 0; $line < count($body); $line++) {
                             echo "<PRE>", HtmlSpecialChars($body[$line]), "</PRE>\n";
                         }
                         echo "<PRE>---Message body ends above---</PRE>\n";
                         if (($error = $pop3->DeleteMessage(1)) == "") {
                             echo "<PRE>Marked message 1 for deletion.</PRE>\n";
                             if (($error = $pop3->ResetDeletedMessages()) == "") {
                                 echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
                             }
                         }
Пример #3
0
 /**
  * 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;
 }