This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 3

HOW TO: Add Mailchimp integration via Mailchimp API

  1. tmparisi

    There are other threads on this forum that show how to add mailchimp integration via HTTP, which is fine if you don't mind confirmation emails. However, I have a dropdown on my form that asks the user if they want to be added to our mailing list, if they select "yes", I don't want them to receive another email asking them to confirm... that is just annoying. In order to bypass this 'double opt-in' functionality, you must make use of the mailchimp API. Here is the solution I created that seems to work flawlessly:

    1. Go get yourself a copy of the API (MCAPI.class.php) from here:

    2. Place the MCAPI.class.php file in your machform/lib/ folder.

    3. Edit machform/includes/post-functions.php

    Do a search for (should be around line 2326):

    if(empty($error_elements) && empty($process_result['custom_error'])){
    $process_result['status'] = true;

    and right below this code, add the following:

    if($form_id == 8 && $table_data['element_8'] == 1){
    	require_once 'lib/MCAPI.class.php';
    	$apikey = 'xxxxxxxxxxxxxxxxxxxxxxx';
    	$listId = 'xxxxxxx';
    	$my_email = $table_data['element_2'];
    	$double_optin = false;
    	$send_welcome = false;
    	$api = new MCAPI($apikey);
    	$merge_vars = Array(
    		'EMAIL' => $table_data['element_2'],
    		'FNAME' => $table_data['element_1_1'],
    		'LNAME' => $table_data['element_1_2']
    	$retval = $api->listSubscribe( $listId, $my_email, $merge_vars, $double_optin, $send_welcome);

    4. Edit the code you just added to match your mailchimp account. That is, enter your API Key, and List ID... both of which can be found at the mailchimp site (you may need to sign up for an API key still).

    5. Edit any restriction parameters. For example, my code only activates when we are dealing with Form #8 and if my dropdown (element_8) is equal to Yes, which is the 1st item in the dropdown, thus it is = 1.

    6. Edit any other element IDs that you want.

    More information on the API and what parameters can be set are available here:

    There may be bugs I am not aware of, but it seems to work quite well. If anyone sees any problems with the above code, please post it here! Hope this helps someone!

    Posted 10 years ago #
  2. radixbv

    seems to work besides on on little nasty detail.
    The mailchimp api log gives a result error.

    To test i made the array to only email adress.

    I'm i missing a mialchimp setting to allow this?

    PS: I changed settings on MCAPI.class.php to 755, as the first api call didn't came trough. If it works i wil test to see if this is needed.

    Posted 10 years ago #
  3. tmparisi

    I am not entirely sure what you are asking. Also, what is the error you are receiving?

    Posted 10 years ago #
  4. radixbv


    Retest it again.
    The problem was the field numbering.

    I deleted the name field and put in a new one with more field. I moved that to the top. It gave field number 1.
    But the input didn't appear in $table_data['element_1_1']
    For some strange reason the email field was also not the assigned number, but one higher.

    I made a new form and then it worked as described as above.

    Right setting as standard (644) are oke.

    To debug output you can include echo fields

    something like:

    if ($api->errorCode){
    echo "\tfname=".$table_data['element_1_1']."\n";
    echo "\tfname=".$table_data['element_1_2']."\n";
    echo "\temail=".$table_data['element_2']."\n";
    } else {
    echo "Subscribed \n";

    Posted 10 years ago #
  5. tmparisi

    So it was an issue with your form's personal element numbering? Of course, this numbering will differ for each individual and from form to form. In any case, I am glad you got it working!

    Posted 10 years ago #
  6. williamansley

    @tmparisi: Thanks a lot for sharing this information on the forum. We don't have any current plans to integrate Mailchimp with Machform, but it is very nice to have this information available, in case we decide to do so in the future.

    Posted 10 years ago #
  7. tmparisi

    Just a note: This code currently only works with Single Page Forms. The Machform team is currently helping me make proper adjustments so it works with multipage forms.

    Posted 10 years ago #
  8. fpgrenier

    Thanks a bunch for sharing this Tmparisi!

    I've been trying to force the subscription to a certain group but, unfortunately, I can't get it to work. Anyone tried this already?

    Posted 10 years ago #
  9. tmparisi

    So you want to force it and disregard the "opt-in" email...? Just want to make sure I understand fully. And, have you been successful with this code despite that issue?

    Posted 10 years ago #
  10. fpgrenier

    Yes, the code works beautifully...

    It currently drops the subscription into the list I have specified with $listId... but within that list, I have a few groups set-up (group A, group B, group C...) and I would love for the subscriptions to automaticaly get inserted into one of those.

    I found some information on the subject (, but I can't seem to get it to work with your script.

    Thanks again!

    Posted 10 years ago #
  11. tmparisi

    I don't know much about groupings as I have not utilized them. However, from a quick glance it seems that a record is not inserted into a group, but rather, the record is given a classification of, in your case, Group A, Group B, or Group C. I am going to assume that this means the user is provided with some type of option in your MachForm, such as a checkbox list of options to be enrolled into.


    User goes to your form, enters their personal information, selects all of their interests from a group of up to 3, clicks submit, user now has record in mailchimp with linkages to the selected interest/groups.

    This is just a guess and has not been tested, but I would try something along the lines of:

    $merge_vars = Array( 'EMAIL' => $table_data['element_2'], 'FNAME' => $table_data['element_1_1'], 'LNAME' => $table_data['element_1_2'] , 'GROUPINGS' => array(array('name' => 'User Interests', 'groups' => 'Group A'),));

    At this point we have defined the record to be correlated with group title "user interests" and group name "group a". However, MachForm wont understand this, so perhaps we could set up some sort of if/then/else statement in PHP to grab the proper group(s) that the user selected on your MachForm.

    if $table_data['element_7'] group a is selected
        if $table_data['element_7'] group b is selected
            if $table_data['element_7'] group c is selected
                $merge_vars = Array( 'EMAIL' => $table_data['element_2'], 'FNAME' => $table_data['element_1_1'], 'LNAME' => $table_data['element_1_2'] , 'GROUPINGS' => array(array('name' => 'User Interests', 'groups' => 'Group A, Group B, Group C'),));
        $merge_vars = Array( 'EMAIL' => $table_data['element_2'], 'FNAME' => $table_data['element_1_1'], 'LNAME' => $table_data['element_1_2'] , 'GROUPINGS' => array(array('name' => 'User Interests', 'groups' => 'Group A, Group B'),));
    $merge_vars = Array( 'EMAIL' => $table_data['element_2'], 'FNAME' => $table_data['element_1_1'], 'LNAME' => $table_data['element_1_2'] , 'GROUPINGS' => array(array('name' => 'User Interests', 'groups' => 'Group A'),));

    Maybe something like this? This assumes your checkboxes are element_7. Also, I am not sure how MachForm deals with checkboxes in terms of how it tells if they are checked or not (E.g. 1 = checked, 0 = not checked?).

    Anyhow, I realize this is VERY rough and not the easiest solution, but it is just off the top of my head. Perhaps someone else may have a little more insight. If you are unable to figure it out, I would suggest either writing a support ticket OR going to as those guys are awesome over there!

    Whatever you figure out, please post your solution here for others to use. Thanks!

    Posted 10 years ago #

RSS feed for this topic