/**
  * Runs through all the MailChimp users for a given list
  *
  * @param string $listId
  */
 protected function processMailChimpUsers($listId)
 {
     $unsubscribe = array();
     $start = 0;
     do {
         $batch = $this->_mailChimp->listMembers($listId, 'subscribed', null, $start, $this->_batchSize);
         $start++;
         foreach ($batch['data'] as $row) {
             if (!$this->userExists($row['email'], $listId)) {
                 $unsubscribe[] = $row['email'];
             }
         }
     } while (count($batch['data']) == $this->_batchSize);
     $unsubscribe = array_chunk($unsubscribe, $this->_batchSize);
     foreach ($unsubscribe as $i => $batch) {
         $batchResult = $this->_mailChimp->listBatchUnsubscribe($listId, $batch, true, $this->_mailChimpOptions['sendGoodby'], $this->_mailChimpOptions['sendNotify']);
         if (!$batchResult) {
             throw new Galahad_MailChimp_Synchronizer_Exception('Error with batch unsubscribe: ' . $this->_mailChimp->errorMessage);
         } else {
             $this->_batchLog[] = "Unsubscribe Batch {$i}: {$batchResult['success_count']} Succeeded";
             $this->_batchLog[] = "Unsubscribe Batch {$i}: {$batchResult['error_count']} Failed";
             if ($batchResult['error_count']) {
                 $this->_batchErrors["Unsubscribe Batch {$i}"] = $batchResult['errors'];
             }
         }
         unset($unsubscribe[$i]);
     }
     unset($batch);
     unset($unsubscribe);
 }
Exemple #2
0
 public function run($args)
 {
     $subscribers = User::model()->findAllByAttributes(array('newsletter' => true, 'previous_newsletter_state' => false));
     $batch = array();
     foreach ($subscribers as $s) {
         $batch[] = array('EMAIL' => $s->email, 'FNAME' => $s->first_name, 'LNAME' => $s->last_name);
         $s->previous_newsletter_state = true;
         $s->save();
     }
     $api = new MCAPI(Yii::app()->params['mc_apikey']);
     $result = $api->listBatchSubscribe(Yii::app()->params['mc_listID'], $batch, false);
     if (isset($result['errors'])) {
         foreach ($result['errors'] as $e) {
             if ($e['code'] == 212) {
                 //User unsubscribed, need to resubscribe individually
                 $user = User::model()->findByAttributes(array('email' => $e['email']));
                 if ($user !== null) {
                     $info = array('FNAME' => $user->first_name, 'LNAME' => $user->last_name);
                     $api->listSubscribe(Yii::app()->params['mc_listID'], $user->email, $info);
                     if ($api->errorCode) {
                         echo "Subscribe {$user->email} failed!\n";
                         echo "code:" . $api->errorCode . "\n";
                         echo "msg :" . $api->errorMessage . "\n";
                     } else {
                         echo "Re-Subscribe {$user->email} Successfully\n";
                     }
                 }
             } else {
                 if ($e['code'] != 214) {
                     print_r($e);
                 }
             }
         }
     }
     $unsubscribers = User::model()->findAllByAttributes(array('newsletter' => false, 'previous_newsletter_state' => true));
     $batch = array();
     foreach ($unsubscribers as $u) {
         $batch[] = $u->email;
         $u->previous_newsletter_state = $u->newsletter = 0;
         $u->save();
     }
     $result = $api->listBatchUnsubscribe(Yii::app()->params['mc_listID'], $batch);
     if (isset($result['errors'])) {
         foreach ($result['errors'] as $e) {
             if ($e['code'] != 215) {
                 print_r($e);
             }
         }
     }
 }
/**
This Example shows how to run a Batch Unsubscribe on a List using the MCAPI.php 
class and do some basic error checking or handle the return values.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php';
//contains apikey
$api = new MCAPI($apikey);
$emails = array($my_email, $boss_man_email);
$delete = false;
//don't completely remove the emails
$bye = true;
// yes, send a goodbye email
$notify = false;
// no, don't tell me I did this
$vals = $api->listBatchUnsubscribe($listId, $emails, $delete, $bye, $notify);
if ($api->errorCode) {
    // an api error occurred
    echo "code:" . $api->errorCode . "\n";
    echo "msg :" . $api->errorMessage . "\n";
} else {
    echo "success:" . $vals['success_count'] . "\n";
    echo "errors:" . $vals['error_count'] . "\n";
    foreach ($vals['errors'] as $val) {
        echo "\t*" . $val['email'] . " failed\n";
        echo "\tcode:" . $val['code'] . "\n";
        echo "\tmsg :" . $val['message'] . "\n\n";
    }
}
?>
 
    if (!isset($report_members[strtolower($member['email'])])) {
        $to_remove[] = $member['email'];
    }
}
if ($DEBUG > 1) {
    bam("LIST MEMBERS:");
    bam($list_members);
    bam("==================");
}
if ($DEBUG && !empty($to_remove)) {
    bam("TO REMOVE:");
    bam($to_remove);
    bam("==================");
}
if (!empty($to_remove) && !$DRYRUN) {
    $api->listBatchUnsubscribe($list_id, $to_remove, true, false, false);
    // delete them completely; don't send goodbye; don't send notification to admin.
}
if (!empty($api->errorMessage)) {
    trigger_error("Mailchimp API Error calling listBatchUnsubscribe(): " . $api->errorMessage, E_USER_ERROR);
}
exit(0);
//// HELPER FUNCTION ////
function getMergeVars($person_data, $email)
{
    static $dummy_person = null;
    if (is_null($dummy_person)) {
        $GLOBALS['system']->includeDBClass('person');
        $dummy_person = new Person();
    }
    $dummy_person->populate(0, $person_data);