Version: .1

Type: Full Script

Category: Other

License: Other

Description: This routine will send emails stored in a database on set numbers of days after a begin date. For instance, if you have a baby-related website, you can have people register their due dates. Then have a series of emails that are sent at predetermined times before and after that date.



<?php
// Email Schedule, By Wayne Zeller
// Licensed Under the IDGADWYDWI licensing model.
// (IDGADWYDWI: I Don't Give A Darn What You Do With It)

// This script depends upon the ezsql.php MySql Wrapper, which you can get here:
// http://www.justinvincent.com/home/articles.php?articleId=2

// All Hail EZSql! Hail! Hail! Hail!
// All Hail Justin Vincent, creator of EzSql! Hail! Hail! Hail!
// (Sorry - had to have a little worship of a guy who has saved me so much time!)

// It also depends on two mySQL tables: esched_members and esched_messages
// 'esched_members' should have, at minimum, these fields:
//    'email' (the email address of the member
//    'begindate' (the date of commencement of the email campaign for this member)
//      I would suggest also having an id field to use as a primary key,
//      but this program won't pay attention to it.
// 'esched_messages' should have, at minimum, these field:
//    'days' (the day number on which this message is sent)
//    'subject' (the subject line of this message's email)
//    'body' (the body of the email for this message)

// Populate those tables however you see fit. On the site this was originally
// written for, esched_members is populated by a web form where people put
// the due date of their baby and their email address. Then we populate 
// esched_messages through PHPMyAdmin. We have some messages set for negative 
// days in order to send messages in the weeks leading up the birth, and some 
// positive for following up in the months after the birth.

// (Just a little public service announcement: If you use this, as we do, to send
// emails around the birth of a baby, be sure to provide a mechanism for the member
// to remove or inactivate their record. Nothing is more horrible than losing a baby
// for some reason and then being constantly reminded by timed emails you can't
// remove yourself from.)

// Run this script every day via cron job, or (for windows) via the scheduler.
// IMPORTANT: ONLY RUN IT ONCE PER DAY, OR YOU WILL DUPLICATE THAT DAYS EMAILS!!!!

// This is an amazingly simple script, but I couldn't find php source code for doing
// this kind of thing anywhere else, so I figured I should share it anyway.
// I was actually very surprised at how small it was when I finished it.

// Anyway, here are the 15 lines of code (barely) after 36 lines of comments. 
// How sad.

include_once("ezsql.php");
$messages = $db->get_results("SELECT days, subject, body from esched_messages where active = 'Y';");
foreach ($messages as $message)
   {
    $days = $message->days;
    $subject = $message->subject;
    $body = $message->body;
    $addresses = $db->get_results("SELECT email from esched_members where TO_DAYS(now())-TO_DAYS(begindate)=$days");
    foreach ($addresses as $address)
        {
         $ToAddr = $address->email;
         mail("$ToAddr", "$subject", "$body");
        }
   }
?>