1

I want to let my server send me e-mails with birthday reminders based on a birthday calendar in a database on the server at my web hosting provider's. I know how to send e-mails from PHP, but only as an action after a button click on the client side. That's also the only way to perform an action I could find.

How can I have my server send me an e-mail on a specific date/time without user interaction?

edit
I don't think my question is a duplicate. The presumed duplicate link refers to a question about cron, and the answer is about the syntax, but it doesn't explain how cron works, or how I can use it for my problem.

Joris Groosman
  • 771
  • 8
  • 23
  • 4
    you can set cron job to send email – Apb Nov 16 '15 at 13:13
  • 2
    Possible duplicate of [php cron job every 10 minutes](http://stackoverflow.com/questions/1830208/php-cron-job-every-10-minutes) – Machavity Nov 16 '15 at 13:16
  • Yes Joris there is an option in server to set cron job file for any interval. You should go for it. – Amit Rajput Nov 16 '15 at 13:17
  • Exactly, you'll be needing to set Cron job for this. And you can set this form your server cpanel. There you be getting a tab ccalled Cron Job and you'll get all the relevant information there itself. – Nehal Nov 16 '15 at 13:18
  • looking at his edit it's not a duplicate of http://stackoverflow.com/q/1830208/3664960 but from http://stackoverflow.com/q/23028783/3664960 – davejal Nov 16 '15 at 13:37
  • @downvoter: please tell us why you downvoted – Joris Groosman Nov 16 '15 at 14:18

3 Answers3

0

To elaborate on the comment given by Apb;

  1. create your php file with the contents (the script part the button)
  2. Use cron for linux os or if you're on windows to execute you're php script

Another option is setcronjob.com answered by Santosh Achari on enter link description here question

Community
  • 1
  • 1
davejal
  • 6,009
  • 10
  • 39
  • 82
0

You can write a php script for execution on the command line. To run a php script from the command line you just use the php command:

php myscript.php

In this example if myscript.php has all the logic to send your emails, then you just setup a cron job to execute the file automatically ever day, or every hour (however often you would like it to execute). Then all you need to do is write the logic in your PHP script to check if its "time" to send one of those emails.

The answer on this post has a great explanation of setting up cron jobs for php scripts.

Community
  • 1
  • 1
jpschroeder
  • 6,636
  • 2
  • 34
  • 34
0

As other said cronjobs are a good way to achieve this.

For example (very rudimental) script.php

include '../connect.php';   
$birthday_list =[];
$today = date("m.d.y"); 


try {           
    $sql = "SELECT `Birthday`, `Name`, `Surname`,  FROM `Friends` WHERE Friends.Birthday = '$today'";               
    foreach ($conn->query($sql) as $row) {          
        $temp = [$row['Birthday'],$row['Name'],$row['Surname']];
        $birthday_list[] =  $temp;                  
                    }    
            }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
    }

foreach ($birthday_list as $el){
    $name = $el[1];
    $surname = $el[2];
    $message = 'Todays is'.$name.' '.$surname.' birthday'; 
    mail('me@example.com', 'My Subject', $message);
}

Then you can run this script as cronjob, some hostings (godaddy for example) allow you to set crons from cpanel, otherwise you have to set crons from your server shell.

Nico
  • 6,259
  • 4
  • 24
  • 40