Hoxephos Hoxephos - 1 year ago 54
PHP Question

PHP: If $date is today, send an email - but only send it once

I'm trying to add something to my website where:

If the date is a certain date, I want it to send me an email; however every time I refresh the page on that day it resends the email. I don't want it to do this - only send the email once.

I know a little about SESSIONS and such but not enough to implement them. This is my code, any help would be appreciated.

TL;DR - How to send an email from the website once if the date is a set date?


$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"){

} elseif ($dateInspect == "26/11/16"){

} elseif ($dateInspect == "24/11/16"){

} else {

Answer Source

The better solution is to use an database to store the flag of sending the email on a specific day.

But, if you running a simple website and don't want to go for database, you can simply create a file after the sent, and always check if that file exists.

$dateInspect = date("05-11-16");
    // .. SEND YOUR E-MAIL

    // create the file
    file_put_contents("mail_sent_".$dateInspect.".txt", "mail sent this day");

Attention: Don't use slashes on date, as it will generate an filename as: mail_sent_05/11/16.txt which is an invalid name, filenames can't have slashes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download