PHP IMAP连接速度

PHP IMAP连接速度,php,imap,Php,Imap,这是我第一次在php中使用imap进行实验 我正在为我的公司制作一个面板,在那里我上传客户的姓名和电子邮件,并向用户发送电子邮件 因此,当用户进入面板并有客户时,面板会检查用户电子邮件帐户并搜索来自客户电子邮件的电子邮件 我的问题是,我第一次加载面板时,它变得非常慢,加载需要5秒到15秒(有时甚至更长),但在第一次加载后,它很快,但是如果我离开10分钟,然后重新加载面板,它会第一次变慢 这是我的密码;对于面板中的每个用户,我调用此函数: function mostrarCorreosRelaci

这是我第一次在php中使用imap进行实验

我正在为我的公司制作一个面板,在那里我上传客户的姓名和电子邮件,并向用户发送电子邮件

因此,当用户进入面板并有客户时,面板会检查用户电子邮件帐户并搜索来自客户电子邮件的电子邮件

我的问题是,我第一次加载面板时,它变得非常慢,加载需要5秒到15秒(有时甚至更长),但在第一次加载后,它很快,但是如果我离开10分钟,然后重新加载面板,它会第一次变慢

这是我的密码;对于面板中的每个用户,我调用此函数:

function mostrarCorreosRelacionados($remitente){
$hostname = '{localhost:143}INBOX';
$correo = explode("|||",$_SESSION['correo']);
$username = $correo[0];
$password = $correo[1];

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

/* Recuperamos los emails */
$emails = imap_search($inbox,'FROM "'.$remitente.'"');


/* Si obtenemos los emails, accedemos uno a uno... */
if($emails) {

    /* variable de salida */
    $output = '';

    /* Colocamos los nuevos emails arriba */
    rsort($emails);

    /* por cada email... */
    $ii=0;
    foreach($emails as $email_number) {

        /* Obtenemos la información específica para este email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        //$body = imap_fetchbody($inbox,$email_number,4);

        $uid = imap_uid($inbox, $email_number);

        $header = imap_header($inbox,$email_number);
        $fromInfo = $header->from[0];
        $replyInfo = $header->reply_to[0];


        $details = array(
            "fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
                ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
            "fromName" => (isset($fromInfo->personal))
                ? $fromInfo->personal : "",
            "replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
                ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
            "replyName" => (isset($replyTo->personal))
                ? $replyto->personal : "",
            "subject" => (isset($header->subject))
                ? $header->subject : "",
            "udate" => (isset($header->udate))
                ? $header->udate : ""
        );

        //$message = mail_mime_to_array($inbox,$email_number);



        /* Mostramos la información de la cabecera del email */

        $output.= '<li>';

        $output.= '<div class="encabezadoMail '.($overview[0]->seen ? 'read' : 'unread').'">';

        $output.= '<span class="subject">Asunto: '.decodificarTexto($details["subject"]).'</span><br />';

        $output.= '<a href="mailto:'.$fromInfo->mailbox.'@'.$fromInfo->host.'" class="from" title="'.$fromInfo->mailbox.'@'.$fromInfo->host.'">'.decodificarTexto($fromInfo->personal).'</a><br />';

        $output.= '<span class="textoChico">'.$overview[0]->date.'</span>';

        $output.= '</div>';



        /* Mostramos el mensaje del email */

        $output.= '<div class="cuerpoMail" id="msg_'.$i.'" style="display:none;">'.utf8_encode(getBody($uid,$inbox)).'</div>';

        $output.= '</li>';




        $ii++;
    }



    print '<ul class="emails"><li class="encabezadoMail"><strong>E-mails</strong></li>'.$output.'</ul>';

} 



/* Cerramos la connexión */

imap_close($inbox);
}
功能MOSTRARCORREOSERLACIONADOS($REMILENTE){
$hostname='{localhost:143}收件箱';
$correo=explode(“| | |,$_会话['correo]”);
$username=$correo[0];
$password=$correo[1];
/*康涅西翁酒店*/
$inbox=imap_open($hostname,$username,$password)或die('No se pudo conectar con:usuario:'。$username。'y clave:'。$password.''.imap_last_error());
/*复发*/
$emails=imap_search($inbox,'FROM'。$remotente.');
/*我收到你的电子邮件,加入你*/
如果($电子邮件){
/*沙利达变量*/
$output='';
/*新科洛卡莫斯酒店*/
rsort(电子邮件);
/*por cada电子邮件*/
二元=0;
foreach($email作为$email\u编号){
/*关于电子邮件的信息*/
$overview=imap\U fetch\U overview($inbox,$email\u number,0);
//$body=imap\u fetchbody($inbox,$email\u编号,4);
$uid=imap\u uid($inbox,$email\u number);
$header=imap_头($inbox,$email_号);
$fromInfo=$header->from[0];
$replyInfo=$header->reply_to[0];
$details=数组(
“fromAddr”=>(isset($fromInfo->邮箱)和&isset($fromInfo->主机))
?$fromInfo->邮箱“@”。$fromInfo->主机:“”,
“fromName”=>(isset($fromInfo->personal))
?$fromInfo->个人信息:,
“replyAddr”=>(isset($replyInfo->邮箱)和&isset($replyInfo->主机))
?$replyInfo->mailbox。“@”。$replyInfo->主机:“”,
“replyName”=>(isset($replyTo->personal))
?$replyto->personal:“,
“主题”=>(isset($header->subject))
?$header->subject:“,
“udate”=>(isset($header->udate))
?$header->udate:“
);
//$message=mail\u mime\u to\u数组($inbox,$email\u number);
/*电子邮箱信息管理系统(Mostramos la información de la cabecera del email)*/
$output.='
  • '; $output.=''; $output.='Asunto:'.decodicertexto($details[“subject”])。
    ; $output.='
    '; $output.=''.$overview[0]->date'; $output.=''; /*莫斯特拉莫斯·埃尔门萨耶·德尔·埃米尔酒店*/ $output.=''.utf8_编码(getBody($uid,$inbox))。'; $output.='
  • '; $ii++; } 打印“
    • 电子邮件”
    • 。$output.
    ”; } /*康涅西翁塞拉莫斯酒店*/ imap_关闭($收件箱); }
    在imap_open中,我尝试了localhost、domain和ip,但似乎都是一样的

    有什么想法吗

    更新至MONKEYZEUS

    我照你说的做了,我得到的是:

    第一客户

    科内西翁:0.0309870243073秒 复发时间:26.7151398659秒

    第二客户

    科内西翁:0.102792978287秒 复发时间:0.0511429309845秒

    第三客户

    科内西翁:0.00676202774048秒 复发时间:0.0503911972046秒


    这只是第一次发生,如果我按F5键,那么我在第一个客户端上的长时间没有发生,它加载速度和其他客户端一样快,如果我等待10分钟,它在第一个客户端上再次变慢,你必须弄清楚什么是最长的时间

    我猜是这一部分:

    /* Intento de conexión */
    $inbox = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());
    
    /* Recuperamos los emails */
    $emails = imap_search($inbox,'FROM "'.$remitente.'"');
    
    因此,请尝试对其进行基准测试:

    $start = microtime(true); // start timer
    
    /* Intento de conexión */
    $inbox = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());
    
    echo 'conexión: '.(microtime(true)-$start).' seconds<br>'; // show results
    
    // ---------------------------------------------------------------------
    
    $start = microtime(true); // start timer again
    
    /* Recuperamos los emails */
    $emails = imap_search($inbox,'FROM "'.$remitente.'"');
    
    echo 'Recuperamos: '.(microtime(true)-$start).' seconds<br>'; // show new results
    
    $start=microtime(真);//启动计时器
    /*康涅西翁酒店*/
    $inbox=imap_open($hostname,$username,$password)或die('No se pudo conectar con:usuario:'。$username。'y clave:'。$password.''.imap_last_error());
    回显“conexión:”(微时间(真)-$start)。“秒
    ”;//显示结果 // --------------------------------------------------------------------- $start=microtime(真);//再次启动计时器 /*复发*/ $emails=imap_search($inbox,'FROM'。$remotente.'); 回显“Reciperamos:”。(微时间(真)-$start)。“秒数
    ”;//显示新结果
    科内西翁可能是最慢的部分。这可能是因为您的邮件服务器

    更新 根据您的基准测试,很明显,
    imap\u search()
    是最慢的部分

    这是因为它正在执行数据库搜索,您可能会遇到以下问题:

    • 您的邮件服务器的硬件非常薄弱
    • 您的邮件服务器有100千兆字节的数据/电子邮件,因此查询速度自然会减慢
    • 邮件服务器上的硬盘空间即将用完

    第二次运行速度快的原因是数据库执行了一种称为缓存的操作,因此查询结果被加载到内存(RAM)中,但10分钟后,缓存被数据库清除,或者数据发生了更改,因此需要重新缓存查询。

    什么是
    “FROM”.$reliente.”“
    看起来像什么?里面有
    WHERE
    子句吗?如果没有
    WHERE
    子句,则当您只需要一个特定用户时,您正在获取每个人的电子邮件。使用echo“Reciperamos”。$reliente.:'。(microtime(true)-$start.“seconds
    ”;//显示新结果我得到这个:conexión:0.00506997108459秒tarashanti19@adinet.com.uy:1.65491604805秒,因为这只发生在一段时间的不活动之后,并且只发生在第一个$汇款人处。。可能是imap服务器正在执行此操作