0

我正在做一个抓取邮件并将它们发送到 MySQL 数据库的程序,它工作正常,但是当我插入 MySQL 数据库时,程序并没有将它们全部插入。我不知道问题是插入还是 imap 连接。

请帮助我,这是我的代码,谢谢。

    <?php
$conexion=mysqli_connect("localhost","user","pass","mails");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$resultados = mysqli_query($conexion,"SELECT * FROM mails ORDER BY id_mail DESC LIMIT 1");

while($wry = mysqli_fetch_array($resultados))
  {
  $last_email = $wry['uid_message'];
  }

mysqli_close($conexion);
?>
<?php 
$imap = imap_open("{XXX.XXX.XXX.XXX/pop3/notls}INBOX", "user@localhost", "pass"); 
if( $imap ) 
{ 
    $num = imap_num_msg($imap);
    if($num >0) {
        if($last_email)
        {
            $lol = $last_email;
            $num_message = $last_email;
        }
        else
        {
            $lol = 1;
            $num_message = $num;
        }
        echo '<div id="body" style="display:none;">';
        do{
        $structure = imap_fetchstructure($imap, $num_message);
        $message = imap_fetchbody($imap, $num_message, 1);
        $header = imap_header($imap, $num_message);
        if($structure->encoding == 3) {
        $message = imap_base64($message);
        } else if($structure->encoding == 4) {
        $message = imap_qprint($message);}
        $uid_mess = $num_message;
        echo $lol."<br><br><br>";
        echo "<pre>".htmlentities($message)."</pre>";
        echo "<br><br><br>";
        echo "<hr><br><br><br>";
        mysqli_query($con,"INSERT INTO mails (body, uid, fecha) VALUES ('".htmlentities($message)."', '".$uid_mess."', '".$header."')");
        $lol = $lol + 1;
        $num_message = $num_message - 1;
        }while($lol <= $num);
        echo '</div><br><br>';
        echo '<hr style="width:50%;"><br><br><center>Readed : '.$lol.' mails of '.$num.' | <a href="delete.php">Delete mails from MySQL Database</a></center>';
    }
    else
    {  echo "0 messages.";
    }
}
else
{
echo "An error occurred";
} 
imap_close($imap); 
?>
4

0 回答 0