Skip to content

Instantly share code, notes, and snippets.

@lennartvdd
Created October 21, 2014 10:15
Show Gist options
  • Save lennartvdd/9888593390f4fd241d53 to your computer and use it in GitHub Desktop.
Save lennartvdd/9888593390f4fd241d53 to your computer and use it in GitHub Desktop.
Mailchimp Subscribe
<?php
/**
* Copyright 2014 by LDiT
* Created by: Lennart van den Dool
* Date: 2014-10-21
*
* Free software, licensed under the BSD License
*
* IMPORTANT:
* this script is not XSS safe. If you need this you will have to implement a CSRF token.
* This requires setting a session, which is not possible for me without access to the frontend framework.
*/
require('MailChimp.php'); //include the MailChimp class from https://github.com/drewm/mailchimp-api/
// == CONFIG ==
// Configure to work with your mailchimp account/mailinglist. You must change these values.
// == REQUIRED SETTINGS
define('MC_API_KEY', '[place your MailChimp api key here]');
define('MC_LIST_ID', false); //set mail list id. Run once without setting this value to print the list (with id's) of mailing lists in your account.
// == OPTIONAL SETTINGS:
//flag to determine whether we replace the interest
//groups with the groups provided or we add the provided groups
//to the member's interest groups (optional, defaults to true)
define('MC_REPLACE_INTERESTS', false);
//flag to control whether a double opt-in confirmation message
//is sent, defaults to true. Abusing this may cause your account to be suspended.
define('MC_DOUBLE_OPTIN', true);
//flag to control whether existing subscribers
//should be updated instead of throwing an error. defaults to false.
define('MC_UPDATE_EXISTING', true);
//if your double_optin is false and this is true,
//we will send your lists Welcome Email if this subscribe succeeds -
//this will *not* fire if we end up updating an existing subscriber.
//If double_optin is true, this has no effect. defaults to false.
define('MC_SEND_WELCOME', false);
// == logic ==
// Change below only if you know what you're doing.
if(!MC_LIST_ID) {
header("HTTP/1.0 500 Internal Server Error");
echo 'Application Misconfigured: missing mail list (MC_LIST_ID).'.PHP_EOL;
showLists();
exit;
}
header("Content-Type: application/json;");
if(!isset($_GET['email']) || empty($_GET['email'])) {
header("HTTP/1.0 400 Bad Request");
echo json_encode(array("error" => "Missing e-mail address"));
exit;
}
$result = subscribe($_GET['email']);
if($result === false) {
header("HTTP/1.0 500 Internal Server Error");
echo json_encode(array("error" => "Invalid response from MailChimp Server. Could not JSON decode."));
exit;
} else {
if(isset($result['status']) && $result['status'] == 'error') {
header("HTTP/1.0 500 Error " . $result['name']);
}
echo json_encode($result);
}
exit;
function subscribe($email)
{
$MailChimp = new \Drewm\MailChimp(MC_API_KEY);
$result = $MailChimp->call('lists/subscribe', array(
'id' => MC_LIST_ID,
'email' => array('email'=>$email),
'merge_vars' => array(),
'double_optin' => MC_DOUBLE_OPTIN,
'update_existing' => MC_UPDATE_EXISTING,
'replace_interests' => MC_REPLACE_INTERESTS,
'send_welcome' => MC_SEND_WELCOME,
));
return $result;
}
function showLists()
{
$MailChimp = new \Drewm\MailChimp(MC_API_KEY);
$result = $MailChimp->call('lists/list');
header("Content-Type: text/plain;");
ini_set('html_errors', 0);
echo "Available lists:".PHP_EOL;
echo sprintf(" % -10s % -10s %s", 'ID', 'web_id', 'name') . PHP_EOL;
foreach($result['data'] as $list) {
echo sprintf(" # % -10s % -10s %s", $list['id'], $list['web_id'], $list['name']) . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment