We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1512
    • 57 Posts
    Hi everybody,

    I’m trying to integrate eForm with the Mailchimp API. Basically, i want it so when someone submits the eform, it takes their first name, surname and email address, submits it to mailchimp thro the api and then carries on as normal with eform.

    I have this so far:

    <?php
    include_once('/var/www/vhosts/domain.com/httpdocs/assets/snippets/mcapi/MCAPI.class.php');
    function signUpEmail(&$fields){
    	if($fields['signup']){
    		include_once('/var/www/vhosts/domain.com/httpdocs/assets/snippets/mcapi/config.inc.php');
    		$api = new MCAPI($apikey);
    
    		$email = $_POST["email"];
    
    		$merge_vars = array('');
    		$retval = $api->listSubscribe( $listId, $email, $merge_vars, '', false, '', '', false);
    		
    		if ($api->errorCode){
    			$m = "$listId - Unable to load listSubscribe()!\n" ."\tCode=".$api->errorCode."\n" . "\tMsg=".$api->errorMessage."\n";
    			$modx->logEvent(29, 1, $m . ' ON ' .$email, 'MailChimp');
    		} else {
    		    //$modx->logEvent(29, 1, $retval . ' ON ' .$email, 'MailChimp');
    		}	
    	}
    }
    ?>
    


    I include the snippet on the page with [!signUpEmail!]
    then submit &eFormOnBeforeMailSent=`signUpEmail` in my eform code.

    I’ve tried to adapt a post from a while back, but it doesnt add the user to mailchimp because its not getting the email address and name data from the eform. Anyone with php skills can prob fix this in a few mins as im 100% sure im overlooking a few things.

    Any advice would be great.

    Thanks
    André
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Instead of the $_POST, try getting the details you want from the $fields array that eForm makes available to your function code. It will contain all of the fields marked with the eform="..." attribute in the form.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 1512
        • 57 Posts
        hey susan,

        thanks for the advice! smiley

        Is the following code along the right lines? My php skills truly are bottom of the barrel im afraid:

        <?php
        include_once('/var/www/vhosts/domain.com/httpdocs/assets/snippets/mcapi/MCAPI.class.php');
        function signUpEmail(&$fields){
        	if($fields['divecentre_signup_large']){
        		include_once('/var/www/vhosts/domain.com/httpdocs/assets/snippets/mcapi/config.inc.php');
        		$api = new MCAPI($apikey);
        
        		$merge_vars = array('');
        		$retval = $api->listSubscribe( $listId, $dbTable['email'], $merge_vars, '', false, '', '', false);
        		
        		if ($api->errorCode){
        			$m = "$listId - Unable to load listSubscribe()!\n" ."\tCode=".$api->errorCode."\n" . "\tMsg=".$api->errorMessage."\n";
        			$modx->logEvent(29, 1, $m . ' ON ' .$dbTable['email'], 'MailChimp');
        		} else {
        		    //$modx->logEvent(29, 1, $retval . ' ON ' .$dbTable['email'], 'MailChimp');
        		}	
        	}
        }
        ?>