imap_fetchbody函数不会提取所有电子邮件

时间:2019-04-11 13:09:12

标签: php imap

我正在尝试使用php构建邮件客户端,将电子邮件从邮件服务器移动到数据库时遇到问题。

我正在使用imap_fetchbody函数。但是,当我尝试计数(使用imap_num_msg)或获取标头(包括imap_fetchbody的'section'参数上的“ 0”)时,它确实会识别所有电子邮件,而没有正文。数据库是本地的。

function getEmails(){
        $query = "SELECT * FROM users";
        $result = mysqli_query($this->connection, $query);
        while($row = mysqli_fetch_assoc($result)) {
            $imapResource = imap_open($row["mailbox"], $row["usernamemail"], $row["passwordmail"], NULL) or die ('IMAP connection error');
        }

        $body_count = imap_num_msg($imapResource);
        echo "There is a total of " . $body_count . " emails in your inbox. <br>"; // This counts REAL total amount of emails

        $emails = imap_search($imapResource, "ALL");
        rsort($emails);
            if(!empty($emails)){ 
                foreach($emails as $email){
                    // Fetch mail content
                    $overview = imap_fetch_overview($imapResource, $email, 0);
                    $overview = $overview[0];
                    $structure = imap_fetchstructure($imapResource, $email);
                    $attachments = array();

                    $id = $overview->uid;
                    $date = $overview->date;
                    $from = $overview->from;
                    $to = $overview->to;
                    $subject = $overview->subject;

                    $seen = $overview->seen;
                    if ($seen == 0){
                        $seen = "Not Seen";
                    } else{
                        $seen = "Seen";
                    }

                    $body = imap_fetchbody($imapResource, $email, "1", FT_PEEK);  

// Check duplicates before uploading
                    $checkQuery = "SELECT ID FROM emails";
                    $result = mysqli_query($this->connection, $checkQuery);
                    if(mysqli_num_rows($result) > 0) {

                        while($row = mysqli_fetch_assoc($result)){



                            $testID = (string)$id;
                            if($testID !== $row["ID"]){
                                // Input into DB
                                $query = "INSERT INTO emails (ID, Date_recieved, From_email, To_email, Subject, Body, Type, Seen) VALUES ('$id', '$date', '$from', '$to', '$subject', '$body', '$emailType', '$seen')";
                                mysqli_query($this->connection, $query);
                            } else {
                                break;

                            }
                        }
                    } else{       
                        // Input into DB
                        $query = "INSERT INTO emails (ID, Date_recieved, From_email, To_email, Subject, Body, Type, Seen) VALUES ('$id', '$date', '$from', '$to', '$subject', '$body', '$emailType', '$seen')";
                        mysqli_query($this->connection, $query);
                    }
}
}

目标是将所有电子邮件插入数据库,然后拉出以供用户显示和管理

已修复,该问题是电子邮件主题中的特殊字符(单引号)。 设置后注意到

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

要修复,请使用 mysqli_real_escape_string。

1 个答案:

答案 0 :(得分:0)

已修复,该问题是电子邮件主题中的特殊字符(单引号)。设置后注意到

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 要修复此问题,请使用 mysqli_real_escape_string