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

Locate Your MailChimp API Key & List ID

  • Visit the Account section of the MailChimp dashboard and select API Keys in the Extras menu. Copy you API key.
  • Find Your List ID: Visit the List pane of the MailChimp dashboard. You’ll find the ID of a list in the list's Settings.
  • You'll also need your FormSmarts account number and Webhook Key.

Example 1: A Simple Mailing 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 MailChimp list.

The Signup Form

You can try the signup form and subscribe to our demo MailChimp list. Although you will get a confirmation email from MailChimp, 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 MailChimp. 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();

// Enter your MailChimp API key and list ID below:
$mailchimp_api_key = "";
$mailchimp_list_id = "";

// Prepare request
list(, $dc) = explode('-', $mailchimp_api_key);
$endpoint = 'https://'.$dc.'.api.mailchimp.com/2.0/lists/subscribe.php';
$data = json_encode(array(
	      'apikey' => $mailchimp_api_key, 
	      'id' => $mailchimp_list_id, 
	      'email' => array('email' => $email)
	      ));
$options = array(
		 'http' => array(
				 'protocol_version' => 1.1,
				 'method' => 'POST',
				 'header' => "Content-type: application/json\r\n".
				             "Connection: close\r\n" .
				             "Content-length: " . strlen($data) . "\r\n",
				 'content' => $data,
				 ),
		 );

// Submit request to MailChimp
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);

Downloads

Webhook Registration

Once you've uploaded all files to your web server, register the URL of the webhook script (i.e. FormSmarts_MailChimp_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 MailChimp 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 MailChimp list. Note that you will only receive a confirmation email from MailChimp. 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 MailChimp, so you can address the recipient by there name in emails.
  • We then send the data to MailChimp.
$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();

// Enter your MailChimp API key and list ID below:

$mailchimp_api_key = "";
$mailchimp_list_id = "";

// Prepare request
list(, $dc) = explode('-', $mailchimp_api_key);
$endpoint = 'https://'.$dc.'.api.mailchimp.com/2.0/lists/subscribe.php';
$data = array(
	      'apikey' => $mailchimp_api_key, 
	      'id' => $mailchimp_list_id, 
	      'email' => array('email' => $email)
	      );
if ($fname && $lname) $data['merge_vars '] = array('FNAME' => $fname, 'LNAME' => $lname);
$json_data = json_encode($data);
$options = array(
		 'http' => array(
				 'protocol_version' => 1.1,
				 'method' => 'POST',
				 'header' => "Content-type: application/json\r\n".
				             "Connection: close\r\n" .
				             "Content-length: " . strlen($json_data) . "\r\n",
				 'content' => $json_data,
				 ),
		 );

// Submit request to MailChimp
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);

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_MailChimp_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 MailChimp. 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