This is my first post and I will try to be clear as possible.
My client would like to store a code for each customer that they have, the code expiry’s after x amount of months, and my client wants me to build a system that stores the “customers name, the code, expiry date and a reminder date”.
the system has to flag up visually on screen for the client to see when a customers reminder date is current date or passed, plus this needs to send a e-mail reminder to the customer.
So far I have developed a MySql database to store the information, I have developed a website that displays a table of information and allows the user to edit or delete, and the user can add a new customer.
Could someone please help me write a script that will check the current date every day and then email the customer a reminder? and how I can I get it to run on the server side?
I my mind this is what I want to do,
Check today’s date
Store date in a variable ($var)
then run Sql query to find customers where reminder date is = to $var
then store results in a array
then for each customer in the array send a reminder e-mail.
If the expiry date is in the table you could have a query to select all customers X days before that date to send the reminder.
"SELECT id, name, email, code, whateverElse FROM customers WHERE expiryDate = DATE_ADD(NOW(), INTERVAL 10 DAY)"
So run daily on a cron job, this would select all customers who’s expiry date is in 10 days time.
You then just need to send the emails to the results.
Just and update, I have managed to send an email with a cronjob, but now I am trying to connect to the database and run a query to select all That match current date (todays date) then to send a email to each user from the results of the query.
Just an update I manage to get it working and I thought I would share my the code.
<?php
$currentDate = date('Y-m-d'); //this will get the current date ie when this was posted 2107-07-06
$host_name = " ";
$database = " ";
$user_name = " ";
$password = " ";
$dbcon = mysqli_connect($host_name, $user_name, $password, $database); //connect to database
if($dbcon->connect_error) die($dbcon->connect_error);
$remind_query1 = "SELECT * FROM Bridges30 WHERE reminder= '$currentDate' "; //Sql query to find users that reminders dates match current date.
if($run1 = $dbcon->query($remind_query1))
{
$rows = $run1->num_rows;
for ($j = 0; $j < $rows; ++$j)//loop through each user in results and send a reminder email.
{
$run1->data_seek($j);
$row = $run1->fetch_array(MYSQLI_NUM);
$to = $row[4]; //gets the user email address
$subject = "Code 30 Reminder";
$message = "Hi ".$row[1]."\nJust a reminder that your code 30 will expiry on the ".$row[6].",\n"
."Please renew your code\n"."\nThank You"."\nBridges Nursery";
$headers = "From: Bridges Nursery";
mail($to,$subject,$message,$headers);
}
}
?>