我试图在我的网站上添加一些内容:
如果日期是特定日期,我希望它给我发电子邮件;但是当我每天刷新页面时,它会重新发送电子邮件。我不希望它这样做 - 只发送一次电子邮件。
我对SESSIONS有一点了解,但还不足以实现它们。这是我的代码,任何帮助将不胜感激。
TL; DR - 如果日期是设定日期,如何从网站发送一次电子邮件?
//Inspections
<?php
$recipients = array("Fake@email.com","Another@FakeEmail.com");
$to = implode(",", $recipients);
$subject = "Inspection Reminder.";
$bedMsg = "There is a BEDROOM inspection in five days!";
$comMsg = "There is a KITCHEN inspection in five days!";
$dateInspect = date("05/11/16"); //Set to a certain date for testing
if ($dateInspect == "05/11/16"){
mail($to,$subject,$comMsg);
} elseif ($dateInspect == "26/11/16"){
mail($to,$subject,$comMsg);
} elseif ($dateInspect == "24/11/16"){
mail($to,$subject,$bedMsg);
} else {
;
}
?>
答案 0 :(得分:0)
更好的解决方案是使用数据库存储在特定日期发送电子邮件的标志。
但是,如果您运行一个简单的网站并且不想使用数据库,您只需在发送后创建一个文件,并始终检查该文件是否存在。
$dateInspect = date("05-11-16");
if(!file_exists("mail_sent_".$dateInspect.".txt")){
// .. SEND YOUR E-MAIL
// create the file
file_put_contents("mail_sent_".$dateInspect.".txt", "mail sent this day");
}
注意:不要在日期使用斜杠,因为它会生成一个文件名:mail_sent_05/11/16.txt
这是一个无效的名称,文件名不能有斜杠。