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.
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';
}
One reply on “Mailchimp 3.0 PHP Add Subscriber Directly To List Without An Opt-In”
// You have commented out the option to subscribe the user directly to your list.
// ‘status’ => ‘pending’ // double opt-in, a confirmation email will be sent to the user
/* you want to use the following */
‘status’ => ‘subscribed’ // single opt-in, no confirmation email will be sent to the user
LikeLike