Have you ever thought about a way to make your members and attendees feel special? Sure, you can personally call members and attendees that complete Connection Cards or Prayer Request forms.
Would you execute a simple program if I could show you how to send year around reminders WITHOUT hiring additional staff to pick up and dial the phones or send emails?
The good news is that today’s tutorial will show you how to create a CCB Birthday emailer. I will not cover the automation portion of this tutorial as it pertains to storing and retrieving your CCB data in an external database, and using a cron job to run the script daily. I’ll cover these automation components in another tutorial. But I digress
This tutorial offers a simple one-off PHP script that will compare and determine whether or not it’s the attendee’s or member’s birthday, and then send a simple email message to the person if it is their birthday. If you’re ready, let’s get started!
Setting up time variables
For today’s tutorial, open your Text Editor of choice and create a php file, naming and saving it as birthday-emailer.php. Next, setup the following time variables used throughout the script: daysInYear, secondsInOneDay, Month, Day and Year.
As you can see below, there are 365 days in a year and 86,400 seconds in one day. To retrieve the month, day and year for the day the script will run or execute, we’ll use PHP’s built-in date function.
<?PHP // Timing Variables // Set the month, day and year variables for today's date $daysInYear = 365; $secondsInOneDay = 86400; $Month = date("m"); $Day = date("d"); $Year = date("Y"); ?>
Now that you have the base variables for today’s time, we’ll piece each together to convert the day into total seconds using PHP’s built-in strtotime function. For birthday, we are only seeking to find the day and not a certain hour, min or second throughout the day (see PHP’s built-in time function), which is why we use ” 00:00:00″ as a part of the birthdayPre variable. Simply, we’re setting time to the beginning of the day or 12 am using ” 00:00:00″.
<? // Convert today to time in seconds $birthdayPre = "$Year-$Month-$Day 00:00:00"; $birthdayPre = strtotime($birthdayPre); $todaysDate = $birthdayPre; ?>
Setting birthday sender and recipient variables
Now that you have your time variables, we’re ready to set the birthday sender and recipient variables.
For the sender variables, we’ll set and use the following variables accordingly: from and from_email.
For the recipient, we’ll use my first and last name, email address, and a fictitious date of birth formatted as YYYY-MM-DD using the following variables: firstname, lastname, email and dob. Feel free to replace my values with your recipient’s information.
<?PHP // Birthday Sender Information $from = 'Your Church or Staff Person'; $from_email = 'fromyourchurch@email.com'; // Birthday Recipient Information $firstname = 'Alvin'; $lastname = 'Brown'; $email = 'test@alvinbrown.com'; $dob = '1972-08-31'; ?>
Convert recipient’s date of birth
Just as you used the timing variables and converted today’s date using the strtotime function, you’ll do the same using the dob variable for the recipient, naming the variable personBirthday.
<?PHP // Convert date of birth to time in seconds $personBirthday = $dob.' 00:00:00'; // really hours, minutes and seconds are not needed, but I do it just to make sure all is well with comparison $personBirthday = strtotime($personBirthday); ?>
Determine if today is the recipient’s birthday
You’ll use an easy if statement that makes use of the php date function to compare today’s date, using the month and day argument (‘m-d’), to the recipient’s month and day, passing the date function both month and day, and the personBirthday variable.
Simply put, if today’s month and day matches the person’s date of birth month and day, then set isBirthday variable equal to the numeric one.
Also, go ahead and set a bdayNotify variable message with the following value: No birthday email sent!. This will be your default message that is echoed to the web browser should it not be the recipient’s birthday.
<?PHP // Determine whether or not it is the person's birthday today by comparing the month and day of person's date of birth to today's month and day if(date('m-d') == date('m-d', $personBirthday)) $isBirthday = 1; // Set a default message that no birthday email was sent $bdayNotify = 'No birthday email sent!'; ?>
Setting up age and birthday email functions
Just as previous tutorials, you’ll continue to create reusable functions in this tutorial. For the first function, you’ll create a function to retrieve the age of the recipient, naming it getAge and passing it the person’s converted birthday in seconds as bday variable argument.
First things first, define the global variables within the function using the following time variables: todaysDate, secondsInOneDay, and daysInYear. Now that you have global variables, you are ready for a some logic.
You need to determine the difference in today’s date in seconds and the person’s date of birth in seconds. To do this, you’ll set a variable named diff, and subtract the person’s date of birth in seconds defined as bday variable from today’s date in seconds defined as todaysDate variable.
Next, you’ll take the difference via the diff variable and divide it by the number of seconds in a day via the secondsInOneDay in variable. Then, take the total and divide it by the number of days in a year via the daysInYear variable. And finally, you’ll use PHP’s built-in floor function to return a whole integer for the person’s age and assigned to the age variable.
And afterwards, simply return the age and that’s the function. Just that easy, that’s it!
<?PHP // This function gets the age of the person function getAge($bday){ global $todaysDate, $secondsInOneDay, $daysInYear; // subtract today's seconds from birthday seconds $diff = $todaysDate - $bday; // now take the difference divided by the number of seconds in a day, then total divided by the number of days in a year // finally, round the number down and you'll have the age $age = floor(( $diff / $secondsInOneDay ) / $daysInYear); return $age; } ?>
The next function you’ll assemble is the is the birthday emailer function defined as sendBDayEmail. This function will accept an array argument that contains the recipient’s information.
First thing first, send the global variables for sender information: from and from_email. Next, set the recipient’s email address as the to variable. Next, you’ll set the subject variable the concatenates both text and the bdayInfo array variables as shown below.
Now you’re ready for the message variable which will include bdayInfo array variables as well. Refer to the message variable below as it’s self explanatory. Notice the use of the .= operator for each line of the birthday message to help keep things simple.
After the message variable is complete, you need to set the MIME version, Content-Type, Encoding type, and who the message is from (i.e., sender) as shown below, using variables as and where needed.
Now that all email send variables are set and complete, you are now ready to use PHP’s built-in mail function. One thing to note is to be sure you have email capabilities setup if you are using a web hosting server or a local development environment.
That said, you’ll set the moreresults variable to use the mail function as pass it the following variables that we setup: to, subject, message and headers.
Finally, you need to set a trigger to verify whether or not the email was successfully sent. For this, we will set a msg variable to 0 for false. If moreresults variable is equal to 1, which is what the mail function return if successful, then we use a if statement to set the msg variable to 1 for true.
Then, end the function by returning the msg variable, and that’s it for the send birthday email function.
<?PHP // This function sends the birthday email function sendBDayEmail($bdayInfo){ global $from, $from_email; // Set the to email variable $to = $bdayInfo['email']; // Set the subject variable $subject = "Happy ".$bdayInfo['ordinalage']." Birthday ".$bdayInfo['firstname']; // Set the message variable $message = "Hi ".$bdayInfo['firstname'].",<br><br>"; $message .= "Just sending you a quick email to wish you a happy ".$bdayInfo['ordinalage']." birthday filled with many wishes and dreams that come true!<br><br>"; $message .= "Regards,<br>"; $message .= "Your Name<br>"; $message .= "Your Church Name"; // Set the email headers $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 7bit\r\n"; $headers .= "From: $from <" . $from_email . ">\r\n"; // Send email $moreresults = mail($to, $subject, $message, $headers); $msg = 0; if($moreresults) $msg = 1; return $msg; } ?>
Ready for recipient birthday logic if it is their birthday?
So now, you are ready for the brains of the script when it is the recipient’s birthday.
For this section, I’ll show you how to prepare the birthday data for the recipient, retrieve the recipient’s birthday age, use a few more PHP built-in functions, and use your user-defined PHP functions we created above.
To start the logic, use a if statement that checks to see if the isBirthday variable is set or not. If the isBirthday variable is set, we proceed, and if not, nothing happens.
One of the first actions inside the if statement is setting the birthdayAge variable, using the getAge function to determine the recipient’s age using the personBirthday variable.
For the next step, you’ll need to use PHP’s NumberFormatter class to format the recipient’s birthday age in ordinal format. Simply set the language using the locale variable, instantiate the class using the ordinalFormat variable, and then set a bdayEmailAge variable to use the format reference passing it the birthdayAge variable.
Next, create a bdayInfo variable to use an empty array. I do this as a precaution to simply ensure the bdayInfo variable is clear and holding no previous data. This will come in handy when you’re dealing with dynamically retrieving multiple recipient records programmatically.
After setting the array to empty, now set the bdayInfo variable as array that binds and associates the following labels to their respective values: firstname > firstname, lastname > lastname, email > email, age > age, and ordinalage > ordinalage.
After setting the bdayInfo variable, now assign a emailSent variable, making it equal to the sendBDayEmail function you created earlier in this tutorial and passing the function bdayInfo array variable.
Finally, you use a If statement to verify if the birthday email was sent. So, if emailSent variable returns 1, then we set the bdayNotify variable message and it now takes the place of the original bdayNotify variable message. If emailSent variable is not sent and 0 is returned, nothing happens.
The last thing to do is echo the bdayNotify variable to the web browser, and you’re done with your birthday emailer.
Now, there are many more advanced actions to add to this simple tutorial. Again, this tutorial is just to get you started down the path of sending automated birthday and even anniversary emails to your church’s attendees and members. Feel free to explore many more options.
<?PHP // If it is person's bday, then get their age and send them a birthday email if($isBirthday){ $birthdayAge = getAge($personBirthday); // This sets the ordinal format of the age $locale = 'en_US'; $ordinalFormat = new NumberFormatter($locale, NumberFormatter::ORDINAL); $bdayEmailAge = $ordinalFormat->format($birthdayAge); // Clear/reset birthday information array $bdayInfo = array(); // Set the birthday information array $bdayInfo = array( "firstname" => "$firstname", "lastname" => "$lastname", "email" => "$email", "age" => "$birthdayAge", "ordinalage" => "$bdayEmailAge" ); // Send email $emailSent = sendBDayEmail($bdayInfo); // Set the $bdayNotify message for who received a birthday email if($emailSent) $bdayNotify = $bdayEmailAge.' birthday email sent to $firstname $lastname.'; } echo $bdayNotify; ?>
Put your codebase together and let’s test
So now, it’s time to put your birthday emailer code together and test. Feel free to place your functions inside a functions file and include it in your birthday-emailer.php file. You don’t have to, but it will help you considerably should you go to reuse the functions for another project. That’s it!
Here’s what you should have:
<?PHP // Timing Variables // Set the month, day and year variables for today's date $daysInYear = 365; $secondsInOneDay = 86400; $Month = date("m"); $Day = date("d"); $Year = date("Y"); // Convert today to time in seconds $birthdayPre = "$Year-$Month-$Day 00:00:00"; $birthdayPre = strtotime($birthdayPre); $todaysDate = $birthdayPre; // Birthday Sender Information $from = 'Your Church or Staff Person'; $from_email = 'fromyourchurch@email.com'; // Birthday Recipient Information $firstname = 'Alvin'; $lastname = 'Brown'; $email = 'test@alvinbrown.com'; $dob = '1972-08-31'; // Convert date of birth to time in seconds $personBirthday = $dob.' 00:00:00'; // really hours, minutes and seconds are not needed, but I do it just to make sure all is well with comparison $personBirthday = strtotime($personBirthday); // Determine whether or not it is the person's birthday today by comparing the month and day of person's date of birth to today's month and day if(date('m-d') == date('m-d', $personBirthday)) $isBirthday = 1; // Set a default message that no birthday email was sent $bdayNotify = 'No birthday email sent!'; // If it is person's bday, then get their age and send them a birthday email if($isBirthday){ $birthdayAge = getAge($personBirthday); // This sets the ordinal format of the age $locale = 'en_US'; $ordinalFormat = new NumberFormatter($locale, NumberFormatter::ORDINAL); $bdayEmailAge = $ordinalFormat->format($birthdayAge); // Clear/reset birthday information array $bdayInfo = array(); // Set the birthday information array $bdayInfo = array( "firstname" => "$firstname", "lastname" => "$lastname", "email" => "$email", "age" => "$birthdayAge", "ordinalage" => "$bdayEmailAge" ); // Send email $emailSent = sendBDayEmail($bdayInfo); // Set the $bdayNotify message for who received a birthday email if($emailSent) $bdayNotify = $bdayEmailAge.' birthday email sent to $firstname $lastname.'; } echo $bdayNotify; // FUNCTIONS // This function gets the age of the person function getAge($bday){ global $todaysDate, $secondsInOneDay, $daysInYear; // subtract today's seconds from birthday seconds $diff = $todaysDate - $bday; // now take the difference divided by the number of seconds in a day, then total divided by the number of days in a year // finally, round the number down and you'll have the age $age = floor(( $diff / $secondsInOneDay ) / $daysInYear); return $age; } // This function sends the birthday email function sendBDayEmail($bdayInfo){ global $from, $from_email; // Set the to email variable $to = $bdayInfo['email']; // Set the subject variable $subject = "Happy ".$bdayInfo['ordinalage']." Birthday ".$bdayInfo['firstname']; // Set the message variable $message = "Hi ".$bdayInfo['firstname'].",<br><br>"; $message .= "Just sending you a quick email to wish you a happy ".$bdayInfo['ordinalage']." birthday filled with many wishes and dreams that come true!<br><br>"; $message .= "Regards,<br>"; $message .= "Your Name<br>"; $message .= "Your Church Name"; // Set the email headers $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 7bit\r\n"; $headers .= "From: $from <" . $from_email . ">\r\n"; // Send email $moreresults = mail($to, $subject, $message, $headers); $msg = 0; if($moreresults) $msg = 1; return $msg; } ?>
In upcoming tutorials, I’ll show you how to take this codebase and make it more reusable and dynamic in nature instead of this one-off codebase that processes a single birthday message.
It’s actually easy to do based on the codebase you’ve produced thus far. Adapting this codebase, you’re a good 85% of the way to a dynamic codebase for multiple recipients.
That’s is for now! Let me know if you have questions or comments, or run into any challenges with this tutorial. Thanks and talk soon.