Skip to content

Instantly share code, notes, and snippets.

@jawinn
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jawinn/7926ee12b17bf2c1d4bc to your computer and use it in GitHub Desktop.
Save jawinn/7926ee12b17bf2c1d4bc to your computer and use it in GitHub Desktop.
Add a Subscribe to MailChimp Newsletter Option on Your Contact Form - PHP
<?php
// SUBSCRIBE TO MAILING LIST OPTION - ADD TO MAILCHIMP USING API
if ( $_POST['emailUpdates'] == 'Yes' )
{
// Include Mailchimp API class
require_once('MCAPI.class.php');
// Your API Key: http://admin.mailchimp.com/account/api/
$api = new MCAPI('YourAPIKeyHere');
// Your List Unique ID: http://admin.mailchimp.com/lists/ (Click "settings")
$list_id = "YourListIDHere";
// Variables in your form that match up to variables on your subscriber
// list. You might have only a single 'name' field, no fields at all, or more
// fields that you want to sync up.
$merge_vars = array(
'FNAME' => $_POST['firstName'],
'LNAME' => $_POST['lastName']
);
// SUBSCRIBE TO LIST
if ( $api->listSubscribe($list_id, $_POST['email'], $merge_vars) === true ){
$mailchimp_result = 'Success! Check your email to confirm sign up.';
} else {
$mailchimp_result = 'Error: ' . $api->errorMessage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment