我正在尝试使用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。
修复问题是电子邮件主题中的特殊字符(单引号)。设置后注意到
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);修复使用mysqli_real_escape_string。