Mailchimp 3.0 PHP Add Subscriber Directly To List Without An Opt-In

My question is about using the Mailchimp 3.0 API and PHP to add a subscriber directly to my mailing list.

The code I’m using (see below) functions as expected and adds the subscriber. However, the subscriber is sent an opt-in email.

screenshot of test

The code is from here: http://www.johnkieken.com/mailchimp-form-using-api-v3-0-and-jquery-ajax/

The desired behavior is to add the subscriber directly to the list without the opt-in email, as well as provide a message on the site they have been successfully subscribed.

My server is running PHP 5.3.16.

I have the HTML file, the Mailchimp API wrapper (mailchimp.php) and subscribe.php all residing in the same directory for testing purposes.

I’m not well versed in coding, so I hope someone can help.

HTML

<!DOCTYPE html>
<html>
<head>
    http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
</head>
<body>
<form id="signup" action="index.html" method="get">
    First Name: <input type="text" name="fname" id="fname" />
    Last Name: <input type="text" name="lname" id="lname" />
    email Address (required): <input type="email" name="email" id="email" />
    <input type="submit" id="SendButton" name="submit" value="Submit" />
</form>
http://jquery.min.js $(document).ready(function() { $('#signup').submit(function() { $("#message").html("Adding your email address..."); $.ajax({ url: 'subscribe.php', // proper url to your "store-address.php" file type: 'POST', // </body> </html>

subscribe.php

<?php // for MailChimp API v3.0

include('MailChimp.php');  // path to API wrapper downloaded from GitHub

use \DrewM\MailChimp\MailChimp;

function storeAddress() {

    $key        = "mymailchimpAPIkey-us17";
    $list_id    = "mymailchimplistid";

    $merge_vars = array(
        'FNAME'     => $_POST['fname'],
        'LNAME'     => $_POST['lname']
    );

    $mc = new MailChimp($key);

    // add the email to your list
    $result = $mc->post('/lists/'.$list_id.'/members', array(
            'email_address' => $_POST['email'],
            'merge_fields'  => $merge_vars,
            'status'        => 'pending'     // double opt-in
            // 'status'     => 'subscribed'  // single opt-in
        )
    );

    return json_encode($result);

}

// If being called via ajax, run the function, else fail

if ($_POST['ajax']) { 
    echo storeAddress(); // send the response back through Ajax
} else {
    echo 'Method not allowed - please ensure JavaScript is enabled in this browser';
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s