function run() { global $ost; if (!$ost->getConfig()->isEmailPollingEnabled()) { return; } //We require imap ext to fetch emails via IMAP/POP3 //We check here just in case the extension gets disabled post email config... if (!function_exists('imap_open')) { $msg = _S('osTicket requires PHP IMAP extension enabled for IMAP/POP3 email fetch to work!'); $ost->logWarning(_S('Mail Fetch Error'), $msg); return; } //Hardcoded error control... $MAXERRORS = 5; //Max errors before we start delayed fetch attempts $TIMEOUT = 10; //Timeout in minutes after max errors is reached. $sql = ' SELECT email_id, mail_errors FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 ' . ' AND (mail_errors<=' . $MAXERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(), mail_lasterror))>' . $TIMEOUT * 60 . ') )' . ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(), mail_lastfetch))>mail_fetchfreq*60)' . ' ORDER BY mail_lastfetch ASC'; if (!($res = db_query($sql)) || !db_num_rows($res)) { return; } /* Failed query (get's logged) or nothing to do... */ //Get max execution time so we can figure out how long we can fetch // take fetching emails. if (!($max_time = ini_get('max_execution_time'))) { $max_time = 300; } //Start time $start_time = Misc::micro_time(); while (list($emailId, $errors) = db_fetch_row($res)) { //Break if we're 80% into max execution time if (Misc::micro_time() - $start_time > $max_time * 0.8) { break; } $fetcher = new MailFetcher($emailId); if ($fetcher->connect()) { db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($emailId)); $fetcher->fetchEmails(); $fetcher->close(); } else { db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($emailId)); if (++$errors >= $MAXERRORS) { //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg = "\n" . _S('osTicket is having trouble fetching emails from the following mail account') . ": \n" . "\n" . _S('User') . ": " . $fetcher->getUsername() . "\n" . _S('Host') . ": " . $fetcher->getHost() . "\n" . _S('Error') . ": " . $fetcher->getLastError() . "\n\n " . sprintf(_S('%1$d consecutive errors. Maximum of %2$d allowed'), $errors, $MAXERRORS) . "\n\n " . sprintf(_S('This could be connection issues related to the mail server. Next delayed login attempt in aprox. %d minutes'), $TIMEOUT); $ost->alertAdmin(_S('Mail Fetch Failure Alert'), $msg, true); } } } //end while. }
function fetchMail(){ global $cfg; if(!$cfg->canFetchMail()) return; //We require imap ext to fetch emails via IMAP/POP3 if(!function_exists('imap_open')) { $msg='PHP must be compiled with IMAP extension enabled for IMAP/POP3 fetch to work!'; Sys::log(LOG_WARN,'Mail Fetch Error',$msg); return; } $MAX_ERRORS=5; //Max errors before we start delayed fetch attempts - hardcoded for now. $sql=' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM '.EMAIL_TABLE. ' WHERE mail_active=1 AND (mail_errors<='.$MAX_ERRORS.' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )'. ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) '; //echo $sql; if(!($accounts=db_query($sql)) || !db_num_rows($accounts)) return; //TODO: Lock the table here?? while($row=db_fetch_array($accounts)) { $fetcher = new MailFetcher($row['userid'],Misc::decrypt($row['userpass'],SECRET_SALT), $row['mail_host'],$row['mail_port'],$row['mail_protocol'],$row['mail_encryption']); if($fetcher->connect()){ $fetcher->fetchTickets($row['email_id'],$row['mail_fetchmax'],$row['mail_delete']?true:false); $fetcher->close(); db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id='.db_input($row['email_id'])); }else{ $errors=$row['mail_errors']+1; db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id='.db_input($row['email_id'])); if($errors>=$MAX_ERRORS){ //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg="\nThe system is having trouble fetching emails from the following mail account: \n". "\nUser: "******"\nHost: ".$row['mail_host']. "\nError: ".$fetcher->getLastError(). "\n\n ".$errors.' consecutive errors. Maximum of '.$MAX_ERRORS. ' allowed'. "\n\n This could be connection issues related to the host. Next delayed login attempt in aprox. 10 minutes"; Sys::alertAdmin('Mail Fetch Failure Alert',$msg,true); } } } }
function fetchMail() { global $cfg; if (!$cfg->canFetchMail()) { return; } //We require imap ext to fetch emails via IMAP/POP3 if (!function_exists('imap_open')) { $msg = 'PHP deve ser compilado com extensão IMAP habilitado para buscar a trabalhar IMAP/POP3!'; Sys::log(LOG_WARN, 'Erro em buscar o email', $msg); return; } $MAX_ERRORS = 5; //Max errors before we start delayed fetch attempts - hardcoded for now. $sql = ' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 AND (mail_errors<=' . $MAX_ERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )' . ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) '; //echo $sql; if (!($accounts = db_query($sql)) || !db_num_rows($accounts)) { return; } //TODO: Lock the table here?? while ($row = db_fetch_array($accounts)) { $fetcher = new MailFetcher($row['userid'], Misc::decrypt($row['userpass'], SECRET_SALT), $row['mail_host'], $row['mail_port'], $row['mail_protocol'], $row['mail_encryption']); if ($fetcher->connect()) { $fetcher->fetchTickets($row['email_id'], $row['mail_fetchmax'], $row['mail_delete'] ? true : false); $fetcher->close(); db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($row['email_id'])); } else { $errors = $row['mail_errors'] + 1; db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($row['email_id'])); if ($errors >= $MAX_ERRORS) { //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg = "\nO sistema está tendo problemas para coletar e-mails da conta do e-mail seguinte: \n" . "\nUsuário: " . $row['userid'] . "\nHost: " . $row['mail_host'] . "\nErro: " . $fetcher->getLastError() . "\n\n " . $errors . ' erros consecutivos. Máximo de ' . $MAX_ERRORS . ' permitido' . "\n\n Isso pode ser problemas de conexão relacionados ao hospedeiro. Próxima tentativa de login em aprox. 10 minutos"; Sys::alertAdmin('Alerta de falha na busca por email', $msg, true); } } } }
function run() { global $ost; if (!$ost->getConfig()->isEmailPollingEnabled()) { return; } //We require imap ext to fetch emails via IMAP/POP3 //We check here just in case the extension gets disabled post email config... if (!function_exists('imap_open')) { $msg = 'osTicket requires PHP IMAP extension enabled for IMAP/POP3 email fetch to work!'; $ost->logWarning('Mail Fetch Error', $msg); return; } //Hardcoded error control... $MAXERRORS = 5; //Max errors before we start delayed fetch attempts $TIMEOUT = 10; //Timeout in minutes after max errors is reached. $sql = ' SELECT email_id, mail_errors FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 ' . ' AND (mail_errors<=' . $MAXERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(), mail_lasterror))>' . $TIMEOUT * 60 . ') )' . ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(), mail_lastfetch))>mail_fetchfreq*60)' . ' ORDER BY mail_lastfetch DESC' . ' LIMIT 10'; //Processing up to 10 emails at a time. // echo $sql; if (!($res = db_query($sql)) || !db_num_rows($res)) { return; } /* Failed query (get's logged) or nothing to do... */ //TODO: Lock the table here?? while (list($emailId, $errors) = db_fetch_row($res)) { $fetcher = new MailFetcher($emailId); if ($fetcher->connect()) { $fetcher->fetchEmails(); $fetcher->close(); db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($emailId)); } else { db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($emailId)); if (++$errors >= $MAXERRORS) { //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg = "\nosTicket is having trouble fetching emails from the following mail account: \n" . "\nUser: "******"\nHost: " . $fetcher->getHost() . "\nError: " . $fetcher->getLastError() . "\n\n " . $errors . ' consecutive errors. Maximum of ' . $MAXERRORS . ' allowed' . "\n\n This could be connection issues related to the mail server. Next delayed login attempt in aprox. {$TIMEOUT} minutes"; $ost->alertAdmin('Mail Fetch Failure Alert', $msg, true); } } } //end while. }
function fetchMail() { global $cfg; if (!$cfg->canFetchMail()) { return; } //We require imap ext to fetch emails via IMAP/POP3 if (!function_exists('imap_open')) { $msg = 'PHP debe ser compilado con la extensión IMAP habilitada para IMAP/POP3 fetch(captura de correo) para que funcione'; Sys::log(LOG_WARN, 'Error de captura de correo', $msg); return; } $MAX_ERRORS = 5; //Max errors before we start delayed fetch attempts - hardcoded for now. $sql = ' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 AND (mail_errors<=' . $MAX_ERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )' . ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) '; //echo $sql; if (!($accounts = db_query($sql)) || !db_num_rows($accounts)) { return; } //TODO: Lock the table here?? while ($row = db_fetch_array($accounts)) { $fetcher = new MailFetcher($row['userid'], Misc::decrypt($row['userpass'], SECRET_SALT), $row['mail_host'], $row['mail_port'], $row['mail_protocol'], $row['mail_encryption']); if ($fetcher->connect()) { $fetcher->fetchTickets($row['email_id'], $row['mail_fetchmax'], $row['mail_delete'] ? true : false); $fetcher->close(); db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($row['email_id'])); } else { $errors = $row['mail_errors'] + 1; db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($row['email_id'])); if ($errors >= $MAX_ERRORS) { //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg = "\nEl sistema está teniendo problemas para obtener los correos electrónicos de la cuenta de correo siguiente: \n" . "\nUsuario: " . $row['userid'] . "\nHost: " . $row['mail_host'] . "\nError: " . $fetcher->getLastError() . "\n\n " . $errors . ' errores consecutivos. Máximo de ' . $MAX_ERRORS . ' permitidos' . "\n\n Esto podría ser una cuestión relacionada con la conexión al host. Siguiente intento en aprox. 10 min"; Sys::alertAdmin('Alerta de fallo en la captura de correo', $msg, true); } } } }