Php script for automatic email by date stored in a db

Hello everyone,
I have made a simple web page where my client can store his legal cases as he is a lawyer. he wants to be notified by email one day before the date of trial session. I built the script in php that would make this and put it in my task scheduler but it doesnt work. i cant receive any email at all.

here is my code

<?php $tomorrow = date("Y-m-d", strtotime("+1 day")); $host_name = "localhost"; $database = "bashkia tirane"; $user_name = "root"; $password = ""; $dbcon = mysqli_connect($host_name, $user_name, $password, $database); if($dbcon->connect_error) die($dbcon->connect_error); $sqlCommand = "SELECT * FROM table 1 WHERE Dt.seances= '$tomorrow' "; if($run1 = $dbcon->query($sqlCommand)) { $rows = $run1->num_rows; for ($j = 0; $j < $rows; ++$j) { $run1->data_seek($j); $row = $run1->fetch_array(MYSQLI_NUM); $to = ""; $subject = "Seancat e ditës së nesërme"; $message = "Nesër, datë ".$row[8]." do të zhvillohet seanca me kod ".$row[0]." ,dergues ".$row[2]. " ,lenda " .$row[3].", paditesi ".$row[4]." , i padituri ".$row[5]. ; $headers = "Email i automatizuar"; mail($to,$subject,$message,$headers); } } ?>

I would really appreciate your help.

Does it send the email if you manually run the code?

Not sure what a “task scheduler” is but you could use a cron job if that is the problem

Its like cron job for windows where you schedule a task.
cron job is better through an online service or using crontab in my command line?

Ah I did think it could be the windows program.

What you should confirm is the code will work anyway to confirm that is not the problem and if so move onto the task scheduler or cron job.

I found out that with a cron job you have to add php in front of the program call or else it will not run. Do you want to paste your task manager command?

I am afraid I do not use a windows server as the permissions/security are a pain. Could that be the problem?

If you’re running it on your local machine, is it configured to properly send email? I guess the first question from @Rubble covers that.

The mail() function is very unreliable and impossible to debug at times. Replace mail() with Swiftmailer. Swiftmailer provides more control over send failures than mail().

The best solution is to use a task scheduler like Jenkins. Projects that have a lot of jobs that run at the command line and on a schedule should absolutely use a task runner like Jenkins over CRON. Jenkins provides much more control over execution and history of jobs making it easier to pinpoint issues and debug.

Online Cronjob Service like Easycron is useful to webmasters. It allows you to launch any script installed on your webserver or shared hosting account at any time. It is a web-based implementation of the Crontab under Unix/Linux and the Task Scheduler under Windows.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.