2

I'm trying to build a mail client with php and I get an issue when moving the e-mails from mail server to my database.

I'm using imap_fetchbody function. However, when I try to count(using imap_num_msg) or get the headers(including "0" on the 'section' parameter of imap_fetchbody) it does recognize all emails, without the body obviously. Database is local.

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);
                    }
}
}

The goal is for all emails to be inserted into the database and then pulled out for display and managed by user

Fixed The problem was special characters in Email's Subject (single-quote). Noticed after setting

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

To fix use mysqli_real_escape_string.

0

Fixed The problem was special characters in Email's Subject (single-quote). Noticed after setting

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); To fix use mysqli_real_escape_string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.