Integrating Mailchimp into Webforms
A few days ago, a client emailed me with an interesting request: Could we modify an existing webform so visitors that fill it out are automatically added to their Mailchimp mailing list?
Hoping for a quick solution, I checked the Mailchimp module config screen. Alas, the module is really only set up to allow add subscribe/unsubscribe checkboxes to the create/edit user screens.
Taking the opposite approach, I checked the Webform configuration screen for the webform in question. Here, I found what I was looking for under the advanced settings panel: a field for additional PHP processing.

Satisfied that I had found a starting point, I let the client know that it could be done, and set out to fill in the details.
An exhaustive Google check turned up very little. The closest solution I found was an incomplete, undocumented code snippet at Csukaj's Blog. In the end, the snippet turned out to be enough to get me pointed in the right direction, so thank you Csukaj, whoever you are!
Here is the final script:
<?php
$data = variable_get( 'mailchimp_lists', NULL);
$lists = array();
if (!empty($data))
$lists = unserialize($data);
$list = $lists[0]
$values = array();
$values['FNAME'] = $form_values['submitted_tree']['contact_person_first_name'];
$values['LNAME'] = $form_values['submitted_tree']['contact_person_last_name'];
$values['EMAIL'] = $form_values['submitted_tree']['contact_email'];
$success = _mailchimp_subscribe_user($list, $values['EMAIL'], $values);
?>Breaking it down, lets start with the first chunk:
<?php
$data = variable_get( 'mailchimp_lists', NULL);
?>Here we use the Drupal API function variable_get() to grab the Mailchimp lists associated with the website through the Mailchimp Module.
<?php
$lists = array();
if (!empty($data))
$lists = unserialize($data);
?>Next, we verify that there is in fact at least one Mailchimp list present in $data, and then copy the serialized data into our $lists array.
<?php
$values = array();
$values['FNAME'] = $form_values['submitted_tree']['contact_person_first_name'];
$values['LNAME'] = $form_values['submitted_tree']['contact_person_last_name'];
$values['EMAIL'] = $form_values['submitted_tree']['contact_email'];
$success = _mailchimp_subscribe_user($list, $values['EMAIL'], $values);
?>In this last section, we sneakily utilize _mailchimp_subscribe_user(), a helper function that comes with the Mailchimp module. This function takes 3 arguments:
- The mailchimp list object ($list)
- The email address of the user to add
- An array containing the all the values we want to add
We already have our first argument, $lists. For the second argument- the email address, we can obtain it from the webform's $form_values variable. An easy way to find the email address is by temporarily adding a print_r($form_values) to the script, and checking the output when you submit the form. Better yet, use dsm($form_values) if you have the Devel module installed. In our case, we found the email address here:
$form_values['submitted_tree']['contact_email']For the third and final argument, we need to build an array of values to submit to Mailchimp. So this would be the email address again, along with other values you want to add to the list such as name, city, zip code, etc. For our form, we needed to collect first name, last name, and email addresses from the form:
<?php
$values = array();
$values['FNAME'] = $form_values['submitted_tree']['contact_person_first_name'];
$values['LNAME'] = $form_values['submitted_tree']['contact_person_last_name'];
$values['EMAIL'] = $form_values['submitted_tree']['contact_email'];
?>Note that the keys to the $values array must match up with your column names in your mailchimp list. So yours may not necessarily be FNAME, LNAME, and EMAIL.
Lastly, we call _mailchimp_subscribe_user() to add user to the mailchimp list:
<?php
$success = _mailchimp_subscribe_user($list, $values['EMAIL'], $values);
?>That's it! Just a small word of caution here- the process I've just described circumvents the 'opt-in' and 'double opt-in' mailing list sign up process that Mailchimp recommends. So its probably a good idea to add some text to your webform alerting users that they will be added to the mailing list. The last thing you want are angry users reporting your email newsletter as spam, jeapordizing your Mailchimp account.
5 comments
You missed one line
You missed explaining the purpose of $list=$lists[0];
Can you let me know what this does? and what if i have multiple lists and want to submit to one particular list?
intergrating Mailchimp into webforms
Hi,
I am very new to coding. Would it be possible to explain how to go about adding this functionality to my site? I also can't seem to even add the embeded signup code to my drupal site. regardless if i use php, html or filtered html.
Any input would be a huge help.
Thanks
Thanks
Su código fué muy útil para solucionar un problema similar.
Mailchimp Webform Module
This module also does that same thing http://drupal.org/project/webform_mailchimp
Drupal module Mailchimp Webform
This drupal module provides a webform component that lets users subscribe to a Mailchimp newsletter as they submit a webform. http://drupal.org/project/webform_mailchimp
Post new comment