Emailing a Queue overdue persons report

Last week, I showed you how to create a Queue overdue list of persons report.

This week, we’re going to work with the same tutorial, yet extend upon the base functionality you developed thus far.

As with any tutorial we extend upon, I’m not going to cover the nitty-gritty details of the previous tutorial. You’ll have a bit of reading and review for areas you find to be challenging.

That said, today’s tutorial focuses on having a Queue overdue persons report emailed to you using Church Community Builder’s API service.

You would right to think that you can already do such reporting by using the existing web front-end interface as provided by CCB.

But what if you wanted to access the data programmatically using a 3rd-party system?

Well, this is where last week’s and today’s tutorials come quite in handy. Let’s get started.

Start with last week’s tutorial codebase

Copy and paste last week’s tutorial codebase into the text editor of your choice, and name and save the queue-overdue-persons-email.php.

Just like previous tutorials, the necessary comments and file inclusions should already exist.

There is one thing you may want to change: functions. Be sure to include the functions in the general.php file for future usage. This should shrink your file size quite a bit.

Time for some slight code modifications

If you remember from the previous tutorial, the code was executed and a HTML table was displayed via the browser with a list of overdue persons for their respective assigned Queue.

To ensure that the same table is emailed, we’ll need to make some slight changes to the code.

Instead of using inline echo of the HTML table, change echo to be a variable. In this case, I’ve declared the variable to be txtOverDue, replacing each echo.

In addition, notice that I’ve added an additional line of code for capturing the date and time when the report was executed using the built-in PHP date method.

This line appears just below the line of code that includes the Total number of Individuals.

Preparing and sending the report via email

It’s been a while since I covered email, but you guessed it, there’s a tutorial for it too. The exact tutorial is creating a CBB birthday emailer.

Once you complete the code modifications above, prepare the following variables with their respective values:

  • to: the email address or email addresses separated by commas (e.g., “,”)
  • from: a from email address
  • from_name: name of person or organization the email should appear from
  • subject: subject of email
  • message: this value should be the txtOverDue variable

Do not make any modifications to the header variable.

Next, declare the moreresults variable and set its value to use the built-in PHP mail method, passing it the following argument variables in their respective order: to, subject, message and headers.

The final step is to determine whether or not the email was successfully sent. For this, we use a simple if statement to check the value returned by the moreresults variable.

Simply put, if the variable returned true, then we echo email was sent successfully. If not, then we echo the email send failed.

And finally, you can echo the message or txtOverDue variable. I put this in place if you choose to execute the code via a web browser and want to see the results (just like last tutorial).

But don’t stop at just emailing the report. Automate this report using a cron job.

That’s it for this tutorial

Now, let’s put everything together, save the file and test the code via a web browser.

Let me know if you have any questions. If not, then I look to see you next week when we’ll take this report one step further.

You’ll have to come back next week to find out the next tutorial’s what, how and why.

Please comment if this tutorial has helped you.