We help IT Professionals succeed at work.

PHP Code that schedules an Email 1 day before the Date inputted.

EasyToHelp
EasyToHelp used Ask the Experts™
on
Kind of like a reminder email from the date they select.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Most Valuable Expert 2011
Top Expert 2016
Commented:
http://www.laprbass.com/RAY_temp_easytohelp.php?s=yesterday
<?php // RAY_temp_easytohelp.php
error_reporting(E_ALL);

date_default_timezone_set('America/Chicago');

$s = (!empty($_GET["s"])) ? trim(strtoupper($_GET["s"])) : FALSE;

if ($t = strtotime($s))
{
    $n = $t - 86400;
    $d = date('r', $n);
    echo  PHP_EOL . "YOUR MESSAGE GOES OUT ON " . $d;
}
else
{
    echo PHP_EOL . "SORRY, I DO NOT UNDERSTAND " . $s;
}

$form = <<<ENDFORM
<form>
<input name="s" />
<input type="submit" value="ENTER A DATE AND CLICK HERE" />
</form>
ENDFORM;

echo $form;

Open in new window

OK, let me make sure you understand that it is very unreliable to schedule the sending of emails just using the code from your site because PHP does not launch unless a page is loaded, so if no one visits your site, your email never gets sent.  You are better off scheduling this through a cron job through your server.

You could store the date that you get using Ray's code above into a database, and then set up a PHP file to be run through CRON to check the database on a regular basis (say every hour or half hour) something like this;

      date_default_timezone_set(get_timezone('America/Denver'));

      $start = time() + (23.5 * 60 * 60);
      $end = time() + (24.5 * 59 * 60);

    $sdate = date('Y-m-d H:i:s', $start);
    $edate = date('Y-m-d H:i:s', $end);
      
      $sql = "SELECT id, user, start FROM ".SCHEDULE_TABLE." WHERE start BETWEEN '" . $sdate . "' AND '" . $edate . "';";
      $emails = $wpdb->mysql_query($sql);
      if (count($emails) > 0) {
                         // send your email
                   }                

Author

Commented:
I dont have a problem finding the previous days date, its more the scheduling of emails.

How would i create the schedule tho ?
OK, did you understand my post?
Most Valuable Expert 2011
Top Expert 2016
Commented:
Agree with OmniUnlimited about the use of a data base table.  The granularity of scheduling the emails is important.  If, for example, you want to send the emails at 0800 on any given day, you might set up a cron job that runs at 0800 each day.

The data base table would have the email_address in a VARCHAR column and the day_to_send in a DATETIME column.  You might want to have an already_sent column with a default value of zero.  The query might look something like this:

$now = date('c');
$then = date('c', strtotime('now - 24 hours');
$sql = "SELECT email_address FROM my_table WHERE already_sent =0 AND day_to_send BETWEEN '$then' AND '$now';

Next your script would iterate over the results set of that query, sending out the email messages.  After all the messages are sent your next query would look something like this:

$sql = "UPDATE my_table SET already_sent = 1 WHERE already_sent =0 AND day_to_send BETWEEN '$then' AND '$now';

You might consider doing the update for each email message as you send it.  That way if the mailer script failed, you could just restart it manually and there would be a reduced risk that you would miss sending some emails.  You could also keep two tables - one of messages to be sent, and one of messages that have been sent.

If some of this is "greek" to you, get this book.  It has great examples and a code library you can copy for your own needs.
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
Most Valuable Expert 2011
Top Expert 2016
Commented:
How would i create the schedule tho ?

The data base has the schedule information in it, in the form of the day_to_send column.  The "cron" job is aware of the DATETIME information on the server.  It selects the information from the data base table based on the contents of the day_to_send column.  The cron job runs once a day, and that is how the schedule is accomplished.

Actually with the design I posted above at ID:36328714 you could run the cron job several times a day without any troubles.

If you want a professional solution this outfit has done a good job for me and my clients:
http://www.constantcontact.com/index.jsp

cheers, ~Ray

Author

Commented:
Thanks guys.

I also found this: with the insert statements

http://www.wickham43.net/phpfuturedates.php