This article describes how to add a person's name and email to a Constant Contact list each time you receive a form submission. We integrate the two online platforms via FormSmarts webhooks API and Constant Contact API. All code samples provided are in PHP and require a very basic understanding of programming and PHP.

Get a Constant Contact Webhook Key & Access Token

Download and Install the Constant Contact SDK

  • Download the Constant Contact PHP SDK and install it on your web host

Example 1: A Simple Contact List Signup Form

The first example describes how to create a simple signup form. Since the sole purpose of the form is to subscribe users to a list, we can assume anyone submitting the form is willing to subscribe, so we'll write a script that simply signs up all users to our Constant Contact list.

The Signup Form

You can try the signup form and subscribe to our demo Constant Contact list. You won't get any emails from FormSmarts via this list, which is only used for demonstration purposes.

The Code

In the PHP code below, we locate an email field in form submission data, then send the field's value to Constant Contact. We've omitted FormSmarts authentication code for clarity. You can download the complete code in the next section.

// Find email field

$email = '';
$fields = $form->{'fields'};
foreach($fields as $field) {
  if ($field->{'field_datatype'} == 'email') {
    $email = $field->{'field_value'};
    break;
  }
}
if (! $email) die();

// require Constant Contact SDK
require_once 'src/Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

// Enter your Constant Contact API key and OAuth Access Token below:
$constant_contact_api_key = "";
$constant_contact_access_token = "";

$cc = new ConstantContact($constant_contact_api_key);

// check if contact already exists
$response = $cc->getContactByEmail($constant_contact_access_token, $email);
if (empty($response->results)) {
  // get contact lists
  $contact_lists = $cc->getLists($constant_contact_access_token);
  // contact doesn't exist, create it
  $contact = new Contact();
  $contact->addEmail($email);
  $contact->addList($contact_lists[0]); // arbitrarilly using the first contact list returned
  $cc->addContact($constant_contact_access_token, $contact, false);
}

Downloads

Webhook Registration

Once you've uploaded all files to your web server, register the URL of the webhook script (i.e. FormSmarts_Constant_Contact_Example_1.php) with FormSmarts.

Example 2: An Online Form with Opt-In Subscription

The second example describes how to selectively subscribe a person to a Constant Contact list on a form which primary purpose is not to sign up subscribers, like the contact form on your site, an online membership form or an event registration form. In all these cases, filling out the form doesn't imply the intent to subscribe to a mailing list, so we should only sign up those users who explicitly indicate on the form they want to.

The Form

Feel free to try out the form for example two. If you check Subscribe to FormSmarts Demo mailing list and submit the form, you will be subscribed to our demo Constant Contact list. FormSmarts will never send you email through this list.

The Code

As before, we've omitted FormSmarts authentication code for clarity. You can download the complete code in the next section. Let's review the script for Example 2:
  • We first find out if the opt-in checkbox is checked. If not, there is nothing more to do, so we stop the script. The checkbox is the fifth field on the form. The first field is at index 0, so the fifth field is at index 4.
  • We then try to find an email field. When doing so, it's important for security reasons to use $field->{'field_datatype'} == 'email' rather than looking up the field by name or index. This will ensure that the field's value is a valid email address.
  • We also try to locate fields called First Name and Last Name. If available, we also send the person's first and last name to Constant Contact, so you can address the recipient by there name in emails.
  • We then send the data to Constant Contact.
$fields = $form->{'fields'};

// 5th field is the opt-in checkbox
if ($fields[4]->{'field_value'} == 'no') die();

// Find email field

$email = '';
$fname = '';
$lname = '';

foreach($fields as $field) {
  if ($field->{'field_datatype'} == 'email') {
    $email = $field->{'field_value'};
  } elseif ($field->{'field_name'} == 'First Name') {
    $fname = $field->{'field_value'};
  } elseif ($field->{'field_name'} == 'Last Name') {
    $lname = $field->{'field_value'};
  }
}
if (! $email) die();

// require Constant Contact SDK
require_once 'src/Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

// Enter your Constant Contact API key and OAuth Access Token below:
$constant_contact_api_key = "";
$constant_contact_access_token = "";

$cc = new ConstantContact($constant_contact_api_key);

// check if contact already exists
$response = $cc->getContactByEmail($constant_contact_access_token, $email);
if (empty($response->results)) {
  // get contact lists
  $contact_lists = $cc->getLists($constant_contact_access_token);
  // contact doesn't exist, create it
  $contact = new Contact();
  $contact->addEmail($email);
  $contact->first_name = $fname;
  $contact->last_name = $lname;
  $contact->addList($contact_lists[0]); // arbitrarilly using the first contact list returned
  $cc->addContact($constant_contact_access_token, $contact, false);
}

Downloads

Note: the following files are the same as the ones in Example 1.

Webhook Registration

After uploading the files to your web server, register the URL of the webhook script (i.e. FormSmarts_Constant_Contact_Example_2.php) with FormSmarts.

Conclusion

In the examples presented here, we've only scratched the surface of what is possible to achieve by integrating FormSmarts forms with Constant Contact. You should customize the webhook script based on your specific requirments. The references below should cover all the material you need, but if you need further assistance, contact FormSmarts support.

References