Today’s tutorial will cover how you can integrate your CCB account to use Mailchimp and Mandrill to send automated email messages.
Before we begin, here’s a quick disclaimer to keep in mind… it is against Mailchimp’s terms of service to send transactional personalized emails. Because of this, Mailchimp allows for transactional personalized emails to be sent via the service Mandrill.
You may be wondering, “What exactly are transactional personalized emails?” Emails like password reminders, order confirmations, receipts, and personalized notifications qualify as transactional emails because they contain personalized information.
Building on the Birthday Emailer Tutorial
Remember the birthday emailer tutorial? Yes, it too qualifies as a transactional email. Now you may be thinking, “Why would I send the birthday emailer when I have existing sending capability?” Great question!
In the birthday emailer tutorial, you’re using good ole’ SMTP and the built-in PHP mailer functionality to send email. The catch to sending email using PHP mailer is that you must have SMTP enabled and properly setup.
Not to mention that your web/email host could block you from sending such messages in bulk. So knowing this, you now have a second option for sending messages using Mailchimp and Mandrill.
In addition to the birthday emailer tutorial, we’ll also build upon the search for individuals and cron job tutorials. Let’s get started!
Integrating Mailchimp and Mandrill
For starters, you’ll need to make sure you have both Mailchimp and Mandrill accounts setup and properly integrated. No worries if you don’t have either set up, I’ve created a quick video that will assist you in getting both set up.
Setting Up Mailchimp Template
I’m not going to spend a lot of time or go into great detail about Mailchimp templates. There are many features one can capitalize on using Mailchimp templates. For more on features and a general overview of Mailchimp templates, review the Template overview in Mailchimp’s Knowledge Base.
Below I’ve provided a quick template overview with screenshots. Log into Mailchimp.
Now click the Templates link, and then click the Create Template button as shown below.
You’re then taken to the following page (see below) to start your template creation. Mailchimp provides some very basic templates to choose from. Ignore those for now and click the Code Your Own link to the right of the Basic link.
Once you’ve clicked Code Your Own link, the following page displayed. It gives you the option to do one of the following:
- Paste in code – simply use an IDE or Text Editor of your choice (check the Toolbox for IDE options) to create your template, and then copy and paste your code into Mailchimp template.
- Import HTML – import a HTML from your computer that contains your template file
- Import Zip – import a zip file containing your template code
For this example, we’ll use the Paste In Code option.
There are two links at the bottom that will allow you to control the content and the look and feel of the template. You can click the Edit design link at the bottom to control attributes about the design (i.e., backgrounds, colors, borders, etc.), and the Edit code link to control your template content.
I recommend copying the default code provided by Mailchimp and editing it to your liking via your IDE or Text Editor. Don’t forget you can use Mailchimp Merge Tags in your content.
Here’s what the Edit design section looks like. Once you’re template is to your liking, you can review your template using the Preview and Test feature in the right hand corner. This allows you to see what your template looks like as well as test send your template via email.
Once your template is complete, you’re now ready to save by clicking Save and Exit in the lower right hand corner. You’ll be asked to provide a template name. In this example, we used Test Message as the name of the Template.
Once you’ve saved your template, you should now see it on the Templates page as you see below. Now you’re ready to import your template to Mandrill.
But before you go to the next step, ensure that you’ve setup and integrated both your Mailchimp and Mandrill accounts.
Now you’re ready to import. Click the Edit button to the far right of your template, and select Send to Mandrill link.
Once your template has been successfully imported, you’ll see it under the Templates section in Mandrill as shown below.
Okay, so now you have one way to setup a template using Mailchimp and then importing to Mandrill. But you can also setup a template in Mandrill without using Mailchimp. I’ll show you how to setup a template in Mandrill next.
Setting Up Mandrill Template
Login to Mandrill and click on the Outbound icon link located on the left hand side of browser (see screenshot below).
Once you reach the Outbound page, click the Templates link and then the Create a Template button.
Give your template a name and click the Start Coding button.
Input all the necessary information into the template fields, and copy and paste your template code from your IDE or Text Editor.
In this template example, I created a simple html birthday message. By the way, just a reminder, you can also use Mailchimp Merge Tags in Mandrill Templates.
And just like Mailchimp, Mandrill has a Preview and Test button (see previous screenshot for button located in upper right hand corner. Once you’ve confirmed your html or text message, simply click the Exit Preview button.
When you’ve completed your template, you are now ready to publish the template clicking the Publish button.
Once published, you’ll be shown the following message as shown in the screenshot below.
And now you have your new template under the Templates section in Mandrill.
Sending Mailchimp and Mandrill Templates to CCB profiles
Okay, I know you’re ready to sink your teeth into some code, so I’m not going to belabor the point of how to retrieve or search for individual profiles using CCB API.
Below, I’ve included a code example of how you can use Mailchimp and Mandrill Templates to send dynamic notification messages, in this case birthday emails, to your CCB profiles.
Here are a few things about the code that you’ll need to know or do to get things up, running, and sending:
- Start here reading Mandrill API Documentation
- Read about and download the Mandrill PHP API
- Obtain your Mandrill API key (see video above)
- Include the Mandrill API library in the include directory and in the config.php file
- You’ll have to make changes to the sendBDayEmail function:
- Set the $template_name variable
- Set and uncomment the $template_content should you want to change the content or use dynamic content via Merge Tags; or you can simply put your content in the Template itself in Mailchimp or Mandrill (see examples above)
Although I didn’t use Merge Tags in this example, one subject I recommend you take time to learn more about is using Mailchimp merge tags in Mandrill. Merge tags will allow you not only to personalize your message, but will open the window to creating and using dynamic content messaging.
For example, does your church use Connection Cards or Guest Cards with various ministry checkboxes (i.e., Membership, Serving, Missions, etc.) for individuals to select? Simply put, you could create a message system using CCB Process and Queues integrated with Mailchimp and Mandrill that sends emails with content associated to ministry checkboxes selected.
Again this code example uses the birthday emailer as a base and combines it with reusable functions and a more streamlined individual profile search. To streamline this code more, you may want to look into and research how to input your CCB profile data into a database, and instead of calling the API, use database queries.
Well, that’s it for this tutorial! I hope and pray this tutorial opens to door to all of the various automated messaging opportunities to bring value to your ministry and to grow your ministry. Let me know if you have questions, and feel free to leave a comment.
<?PHP // contains resusable globals and functions include("includes/config.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; $apiService = 'individual_search'; // CCB api service $urlData = array_filter( array( 'srv' => "$apiService" ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//individuals/individual'); // xpath for individuals->individual $response_object = ''; // Set a default message that no birthday email was sent $bdayNotify = 'No birthday email sent!'; foreach ($nodes as $node){ //$response_object .= $node->full_name.' ('.$node['age'].')'."<br/>"; // now print the person's full name and age (See CCB API documentation for more $apiService fields) // Birthday Recipient Information $bdayNotify = ''; $full_name = $node->full_name; $firstname = $node->first_name; $lastname = $node->last_name; $email = $node->email; $dob = $node->birthday; // 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 it is person's bday, then get their age and send them a birthday email if(date('m-d') == date('m-d', $personBirthday)){ $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 $mandrill; try { $template_name = 'Testing'; /* $template_content = array( array( 'name' => 'testing', 'content' => 'test123' ) ); */ $message = array( 'to' => array( array( 'email' => $bdayInfo['email'], 'name' => $bdayInfo['firstname'].' '.$bdayInfo['firstname'], 'type' => 'to' ) ), 'headers' => array('Reply-To' => 'message.reply@example.com'), 'important' => false, 'track_opens' => true, 'track_clicks' => true, 'auto_text' => null, 'auto_html' => null, 'inline_css' => null, 'url_strip_qs' => null, 'preserve_recipients' => null, 'view_content_link' => null, 'bcc_address' => null, 'tracking_domain' => null, 'signing_domain' => null, 'return_path_domain' => null ); $async = false; $ip_pool = 'Main Pool'; $send_at = ''; $result = $mandrill->messages->sendTemplate($template_name, $template_content, $message, $async, $ip_pool, $send_at); $msg = $result; } catch(Mandrill_Error $e) { // Mandrill errors are thrown as exceptions $msg = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage(); // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123' throw $e; } return $msg; } ?>