所以我正在尝试存储来自gmail的电子邮件,我已经连接了所有东西,并且设法将所有电子邮件内容存储到数据库中,但是现在我遇到的问题是,每次刷新页面时,相同的电子邮件都存储在数据库中。
$bodyText = imap_fetchbody($inbox,$email_number,1.2);
if(!strlen($bodyText)>0){
$bodyText = imap_fetchbody($inbox,$email_number,1);
}
$subject = imap_headerinfo($inbox,$email_number);
$subject = $subject->subject;
$stripped = str_replace('*', '<br>', $bodyText);
$sql = "SELECT id, subject, body FROM emails";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$email_subject = $row['subject'];
if ($subject == $email_subject) {
echo "Email already in database";
}else{
$sql = "INSERT INTO emails (subject, body)
VALUES ('".$subject."', '".$stripped."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
} else {
echo "0 results";
}
所以我尝试使用它
if ($subject == $email_subject) {
echo "Email already in database";
}else{
$sql = "INSERT INTO emails (subject, body)
VALUES ('".$subject."', '".$stripped."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
应该检查数据库中是否有相同主题的电子邮件,如果显示,则显示“电子邮件已在数据库中”,是否不显示“成功创建新记录” < / p>
输入错误消息后,我会在数据库中不断收到相同的电子邮件,因此每次我将电子邮件的整个收件箱刷新到数据库中时