php for循环错误500

时间:2014-04-21 17:51:13

标签: php mysql imap

我正在创建一个小脚本,用于读取收件箱中的电子邮件并将其保存在mysql数据库中。

我有一个检查电子邮件并将它们保存到数据库的PHP,如果它运行超过1秒就打破FOR,并且我有一个ajax脚本,每次停止时调用php。

所以这里是ajax部分:

<script type="text/javascript">
$(document).ready(function(e) {
chequearCorreo(1);
});

function chequearCorreo(inicio){
$.ajax({
  type: 'POST',
  url: 'inc/a.php',
  data: {mailInicio: inicio},
   success: function(data) {
       resultado = data.split("/");
       if(resultado[0]!=resultado[1]){
           //aun no termino
           chequearCorreo(resultado[0]);
       }
       $("body").html(data);

    },
    error: function(data){

    },
 contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
 dataType: 'html'
});
}
</script>

这里是php

session_start();
include 'coneccion-base-mails.php';

$crear_tabla_web =
'CREATE TABLE IF NOT EXISTS web  
(
id INT NOT NULL AUTO_INCREMENT,
uid int,
carpeta VARCHAR(200),
fecha DATETIME,
remitente VARCHAR(200),
destinatario VARCHAR(200),
cc VARCHAR(200),
cco VARCHAR(200),
asunto VARCHAR(200),
body TEXT,
adjuntos VARCHAR(200),
leido VARCHAR(20),
flaged VARCHAR(20),
respondido VARCHAR(20),
reenviado VARCHAR(20),
PRIMARY KEY(id)
)';
$mysqli->query($crear_tabla_web);

//FUNCION GUARDAR MAIL!!!

function guardarMail($uid,$carpeta,$fecha,$remitente,$destinatario,$cc,$cco,$asunto,$body,$leido,$flaged,$respondido,$reenviado){
global $mysqli;
//primero chequeamos que el mail no exista ya en la base
if ($result=$mysqli->query("SELECT * FROM web WHERE uid=".$uid."")){
    if( $result->num_rows == 0 ){
        //guardamos el post
        $fecha = strtotime($fecha);
        $fecha = date("Y-m-d H:i:s", $fecha);

        $cons  = "INSERT INTO web (uid,carpeta,fecha,remitente,destinatario,cc,cco,asunto) VALUES (".$uid.",'".$carpeta."','".$fecha."','".$remitente."','".$destinatario."','".$cc."','".$cco."','".$asunto."')";
        $mysqli->query($cons);
    }
}
}
//fin guardar mail

$hostname = '{localhost:143}Inbox';
$username = '##########';
$password = '###';


$tiempoInicio = microtime(true);
/* Intento de conexión */
$conn = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());

$numMsg = imap_num_msg($conn);

for($i=$mailInicio;$i<=$numMsg;$i++){

$header = imap_header($conn,$i) ;
$fromInfo = $header->from[0];
$replyInfo = $header->reply_to[0];
$toInfo = $header->to[0];
$ccInfo = $header->cc[0];
$bccInfo = $header->bcc[0]; 

$detalles = array(
    "mailRemitente" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
        ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
    "nombreRemitente" => (isset($fromInfo->personal))
        ? $fromInfo->personal : "",
    "mailDestinatario" => (isset($toInfo->mailbox) && isset($toInfo->host))
        ? $toInfo->mailbox . "@" . $toInfo->host : "",
    "nombreDestinatario" => (isset($toInfo->personal))
        ? $toInfo->personal : "",
    "mailRespuesta" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
        ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
    "nombreRespuesta" => (isset($replyTo->personal))
        ? $replyto->personal : "",
    "mailCc" => (isset($ccInfo->mailbox) && isset($ccInfo->host))
        ? $ccInfo->mailbox . "@" . $ccInfo->host : "",
    "nombreCc" => (isset($ccInfo->personal))
        ? $replyto->personal : "",
    "mailCco" => (isset($bccInfo->mailbox) && isset($bccInfo->host))
        ? $bccInfo->mailbox . "@" . $bccInfo->host : "",
    "nombreCco" => (isset($bccInfo->personal))
        ? $bccInfo->personal : "",
    "asunto" => (isset($header->subject))
        ? $header->subject : "",
    "fecha" => (isset($header->udate))
        ? $header->udate : ""
);

$uid = imap_uid($conn,$i);
guardarMail($uid,'Inbox',$detalles["fecha"],$detalles["mailRemitente"],$detalles["mailDestinatario"],$detalles["mailCc"],$detalles["mailCco"],$detalles["asunto"],'','','','','');

$tiempoActual = microtime(true);

$deltaTiempo = $tiempoActual - $tiempoInicio;


if($deltaTiempo > 1 ){


    break;

}



}
echo $i.'/'.$numMsg;

问题是该脚本运行了大约5次,但在某些时候ajax无法加载php,它会给出错误500

1 个答案:

答案 0 :(得分:0)

主机的技术支持将php版本从5.2更改为5.3,现在可以正常运行。它的工作原理似乎运行得更快。