SENDING MEMBERSHIP REMINDER EMAILS USING A CRON JOB - Apr 10th, 2019


SENDING THE MEMBERSHIP RENEWAL REMINDER EMAILS
And once you’ve got your cron job working, you can substitute the PHP script that sends reminder emails to your
members.

It uses the mail function built in to CMS Builder which seems to be more robust then using sendmail directly.

*** Note: if you have issues with sending mail using cron jobs, see the recipe under Bugs and Fixes called: "Cron issues
sending mail with CMSB 3.50 or earlier, and PHP 7.2 after a Bluehost server upgrade"

For reference, the basic implementation for the mail function is:


$message = 'This is a test message';
$mailArray = array(
'to' => 'example@example.com',
'from' => 'example@example.com',
'subject' => 'This is a subject',
'html' => $message
);
$errors = sendMessage($mailArray);


THE MEMBERSHIP RENEWAL REMINDER SCRIPT
Here’s the PHP script that I run once a day to send a renewal reminder emails to members:
A) when their membership is 30 days from expiration, and
B) when their membership is one week from expiration.

To allow my clients to change both the reminder schedule and the text of the reminder emails that are sent, I created 2
text fields and 2 text boxes in a single record editor I named “Common Information”. The text fields are named
first_reminder_day and second_reminder_day, the text boxes are named first_reminder_message and second_reminder_message.

The script allows for html code to be added to the email so that the emails can be styled. It also includes code from
the recipe: “INCLUDING VARIABLES IN THE CONTENTS OF A TEXT BOX” so that the current annual renewal fee can be pulled
from another table called “become_a_member” and included in the “reminder message” fields.

NOTE: don’t forget to change the path to your server’s PHP interpreter and to revise the paths to your server and
any links. Also, make sure that this file has a permission of 755 (read, write, execute) or the file may not run when
called by the cronjob manager.



#!/hsphere/shared/php5/bin/php-cgi -q
<?php
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('your_server_path/','','../','../../','../../../');
foreach (
$dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!
function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

list(
$common_informationRecords, $common_informationMetaData) = getRecords(array(
'tableName' => 'common_information',
'allowSearch' => '0',
'limit' => '1',
));
$common_informationRecord = @$common_informationRecords[0]; // get first record

list($accountsRecords, $accountsMetaData) = getRecords(array(
'tableName' => 'accounts',
));
list(
$become_a_memberRecords, $become_a_memberMetaData) = getRecords(array(
'tableName' => 'become_a_member',
'allowSearch' => '0',
'limit' => '1',
));
$become_a_memberRecord = @$become_a_memberRecords[0]; // get first record
?>
<?php foreach($accountsRecords as $record) : ?>

<?php
$secondsOld = time() - strtotime($record['expiresDate']); // seconds to or since expiration date
$daysOld = intval($secondsOld/60/60/24); // converts seconts to days
$first_reminder_day = $common_informationRecord['first_reminder_day'] ;
$second_reminder_day = $common_informationRecord['second_reminder_day'] ;
// replace the variable *regular_dues_amount* with the annual dues
$first_reminder_message =
str_replace('*regular_dues_amount*',$become_a_memberRecord['regular_dues_amount'],$common_informationRecord['first_reminder_message']);
$second_reminder_message =
str_replace('*regular_dues_amount*',$become_a_memberRecord['regular_dues_amount'],$common_informationRecord['second_reminder_message']);
$member = $record['first_name'] ;

?>


<?php ob_start(); // start capturing output ?>

<?php $first_reminder_message = wordwrap( $first_reminder_message, 70); ?>
<?php echo $first_reminder_message ?>
<?php $output = ob_get_clean(); // stop capturing output ?>

<?php ob_start(); // start capturing output ?>

<?php $second_reminder_message = wordwrap( $second_reminder_message, 70); ?>
<?php echo $second_reminder_message ?>
<?php $output2 = ob_get_clean(); // stop capturing output ?>


<?php if((!@$record['neverExpires'] && @$record['lifetime_member'] == '0') && $record['remove_me'] == '0' && ($daysOld
== -$first_reminder_day)) :?>

<?php $the_to = $record['email']; ?>

<?php

$message = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.special {font-family:Arial; color: rgb(227,224,219); font-size: 1.0em; font-weight: bold; text-decoration: underline;}
.body-text {font-family:Arial; font-size: 1.0em;}
.body-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.0em;}
.heading-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.3em;}
</style>
</head>
<body bgcolor="#7C7164">
<table style='background-color: #7C7164;' width='100%' align='center' border='0' >
<tr>
<td align='left' >
<table style='background-color: #7C7164;' width='70%' align='center' border='0' >
<tr>
<td align='left' >
<div align='left'><img src='http://www.artistsofpalmbeachcounty.org/images/APBC-LOGO.png' width='800' height='183'
style='border:hidden'/></div>
<br /> <br />
<div align='left' class='body-text'>
Hello $member,
<br /> <br />

$output \r\n
<br /> <br />
Best,
<br /> <br />
The Membership Committee <br />
Artists of Palm Beach County\r\n</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
EOF;
$the_from = "membership@artistsofpalmbeachcounty.org";
$the_subject = "Your Current Artists of Palm Beach County Membership will expire in $first_reminder_day days";

$mailArray = array(
'to' => $the_to,
'from' => $the_from,
'subject' => $the_subject,
'html' => $message
);
$errors = sendMessage($mailArray);
// delay execution by .2 sec
usleep(200000);
// reset max_execution_time to 30 sec
set_time_limit(30);
?>

<?php endif ;?>

<?php if((!@$record['neverExpires'] && @$record['lifetime_member'] == '0') && $record['remove_me'] == '0' && ($daysOld
== -$second_reminder_day)) :?>
<?php $the_to = $record['email']; ?>
<?php

$message = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.special {font-family:Arial; color: rgb(227,224,219); font-size: 1.0em; font-weight: bold; text-decoration: underline;}
.body-text {font-family:Arial; font-size: 1.0em;}
.body-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.0em;}
.heading-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.3em;}
</style>
</head>
<body bgcolor="#7C7164">
<table style='background-color: #7C7164;' width='100%' align='center' border='0' >
<tr>
<td align='left' >
<table style='background-color: #7C7164;' width='70%' align='center' border='0' >
<tr>
<td align='left' >
<div align='left'><img src='http://www.artistsofpalmbeachcounty.org/images/APBC-LOGO.png' width='800' height='183'
style='border:hidden'/></div>
<br /> <br />
<div align='left' class='body-text'>
Hello $member,
<br /> <br />

$output2 \r\n
<br /> <br />
Best,
<br /> <br />
The Membership Committee <br />
Artists of Palm Beach County\r\n</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
EOF;
$the_from = "membership@artistsofpalmbeachcounty.org";
$the_subject = "Your Current Artists of Palm Beach County Membership will expire in $second_reminder_day days";

$mailArray = array(
'to' => $the_to,
'from' => $the_from,
'subject' => $the_subject,
'html' => $message
);
$errors = sendMessage($mailArray);
// delay execution by .2 sec
usleep(200000);
// reset max_execution_time to 30 sec
set_time_limit(30);
?>

<?php endif ;?>
<?php endforeach ?>


THE CONTENTS OF MY REMINDER MESSAGE TEXT BOXES

We wanted to let you know that your membership will expire in about a week.

To continue enjoying the benefits of membership, please <a href='http://www.your_site.com/paypal.php' / >CLICK
HERE</a> to renew for another year for only $*regular_dues_amount* using PayPal.

and

We wanted to let you know that your membership will expire in about a month.

To continue enjoying the benefits of membership, please <a href='http://www.your_site.com/paypal.php' / >CLICK
HERE</a> to renew for another year for only $*regular_dues_amount* using PayPal.

GOING FURTHER
For situations where the sending of repetitive emails need to be avoided, Dave Edis from Interactive Tools suggested
setting up check boxes that the PHP script writes to when an email is sent and then including a compare in the if
statements to determine the check boxes value (1=sent, 0=not sent). he reminded that you'd also need to change the value
of those check boxes to "0" on renewal so the process could repeat.

If you're getting error messages in your Cron Daemon emails like these:



Notice: Undefined index: SCRIPT_FILENAME in
/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/plugins/createPDF/createPDF.php on line 22

Notice: Undefined index: HTTP_HOST in
/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/plugins/createPDF/createPDF.php on line 23

Notice: Undefined index: SCRIPT_NAME in
/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/plugins/createPDF/createPDF.php on line 23

Notice: Undefined variable: _SESSION in
/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/lib/login_functions.php on line 88



Dave Edis offers a solution:

He says:

For the Undefined notices, a workaround might be to edit /lib/init.php and add this line just above "// define
constants"



error_reporting(E_ALL & ~E_NOTICE); // show all errors but notices

// define constants



Cron Tab Help Document (IXWebhosting.com)

The format of a cron command is very much the V7 standard, with a number of upward-compatible extensions. Each line has
five time and date fields, followed by a user name if this is the system crontab file, followed by a command. Commands
are executed by cron(8) when the minute, hour, and month of year fields match the current time, and when at least one of
the two day fields (day of month, or day of week) match the current time (see ``Note'' below). Note that this means that
non-existent times, such as "missing hours" during daylight savings conversion, will never match, causing jobs scheduled
during the "missing times" not to be run. Similarly, times that occur more than once (again, during daylight savings
conversion) will cause matching jobs to be run twice.

cron(8) examines cron entries once every minute.

The time and date fields are:

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

A field may be an asterisk (*), which always stands for ``first-last''.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For
example, 8-11 for an ``hours'' entry specifies execution at hours 8, 9, 10 and 11.
Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples:
``1,2,5,9'', ``0-4,8-12''.

Step values can be used in conjunction with ranges. Following a range with ``/'' specifies skips of the number's value
through the range. For example, ``0-23/2'' can be used in the hours field to specify command execution every other hour
(the alternative in the V7 standard is ``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also permitted after an asterisk,
so if you want to say ``every two hours'', just use ``*/2''.

Names can also be used for the ``month'' and ``day of week'' fields. Use the first three letters of the particular day
or month (case doesn't matter). Ranges or lists of names are not allowed.

The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line, up
to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the
cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters,
and all data after the first % will be sent to the command as standard input.

Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. If both fields
are restricted (ie, aren't *), the command will be run when either field matches the current time. For example, ``30 4
1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service