Situation

To automate the newsletter sign-up process from your FormBuilder forms with Campaign Monitor

Brief

To integrate Campaign Monitor API PHP Wrapper into a website running CMS Made Simple version 1.5.1 and FormBuilder 0.5.5 (works with more recent versions as well)

Prerequisites

  • Download the PHP Wrapper for the Campaign Monitor API and upload CMBase.php to your website
  • Have a form in CMS Made Simple FormBuilder ready to test this on

Step by step

  1. In your form, give the fields you want to capture in Campaign Monitor an alias, this is on the Advanced Settings tab which is available when editing the form field. You will need a field called newsletter with the alias ‘newsletter’.
  2. Create a new User Defined Tag (UDT) called “CMsubscriberAdd” and insert the following code. Fill in your Campaign Monitor api_key, client_id and list_id, these are available from the Campaign Monitor interface.
    $FirstName = $params['firstname'];
    
    $LastName = $params['lastname'];
    
    $name = $FirstName . " " . $LastName;
    
    $name = ucwords($name);
    
    $email = $params['email'];
    
    $newsletter = $params['newsletter'];
    
    if ($newsletter == 'Checked')   {
    
    require_once('/home/location/of/CMBase.php');
    
    // The following keys are available from Campaign Monitor
    $api_key = 'enter_your_API_key_here';
    $client_id = 'enter_your_CLIENT_key_here';
    $campaign_id = null;
    $list_id = 'enter_your_LIST_key_here';
    $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
    
    $result = $cm->subscriberAdd($email, $name);
    
    /*  Uncomment the below to get a better idea of what is going on
    if($result['Result']['Code'] == 0)
    echo 'Success';
    else
    echo 'Error : ' . $result['Result']['Message'];
    */
    }
  3. Back to editing the form make sure form submission is set to display the submission template. In the submission template tab, put the following code at the beginning to call your UDT (remember to change the $variables to the field alias you used in the form).
    {CMsubscriberAdd firstname="$first_name" lastname="$last_name" email="$email" newsletter="$newsletter"}
  4. Test the form, it should connect with the Campaign Monitor API and if the subscriber is not on the list, run through either single or double opt-in (however you have Campaign Monitor setup). If the email is in the list, but the name has changed it will update the name.

This method can be expanded to include other information (custom fields).