/**
  * 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);
 }
Example #2
0
 public function moveSubscriber()
 {
     $api = new MCAPI($this->_apiKey);
     $retval = $api->lists();
     if ($api->errorCode) {
         Mage::getSingleton('core/session')->addError($this->__('Error: %s', $api->errorMessage));
     } else {
         foreach ($retval['data'] as $val) {
             if ($val['name'] === "Prospects") {
                 $prospectListId = $val['id'];
             } elseif ($val['name'] === "Fashion Eyewear Customers") {
                 $customerListId = $val['id'];
             }
         }
         if ($api->errorCode != '') {
             // an error occurred while logging in
             Mage::log('Error Code: ' . $api->errorCode);
             Mage::log('Error Message: ' . $api->errorMessage);
             die;
         }
         $retval = $api->listMembers($prospectListId, 'subscribed', null, 0, 10000);
         //var_dump($retval);die;
         if (!$retval) {
             Mage::log('Error Code: ' . $api->errorCode);
             Mage::log('Error Message: ' . $api->errorMessage);
         } else {
             foreach ($retval['data'] as $member) {
                 //echo $member['email']." - ".$member['timestamp']."<br />";
                 $customerModel = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('email', $member['email'])->setPage(1, 1);
                 $customers = $customerModel->getData();
                 $customerId = $customers[0]['entity_id'];
                 $orders = Mage::getResourceModel('sales/order_collection')->addFieldToSelect('*')->addFieldToFilter('customer_id', $customerId)->getData();
                 if (!empty($orders)) {
                     //if ($member['email'] === '*****@*****.**') {
                     $memInfo = $api->listMemberInfo($prospectListId, array($member['email']));
                     $firstname = $memInfo['data'][0]['merges']['FNAME'];
                     if (empty($firstname)) {
                         $firstname = Mage::getModel('customer/customer')->load($customerId)->getFirstname();
                     }
                     $retuns = $api->listUnsubscribe($prospectListId, $member['email'], 1);
                     if (!$retuns) {
                         Mage::log('Unsubscribe Error Code: ' . $api->errorCode);
                         Mage::log('Unsubscribe Error Message: ' . $api->errorMessage);
                     }
                     $merge_vars = array('FNAME' => $firstname);
                     $retsub = $api->listSubscribe($customerListId, $member['email'], $merge_vars);
                     if (!$retsub) {
                         Mage::log('Subscribe Error Code: ' . $api->errorCode);
                         Mage::log('Subscribe Error Message: ' . $api->errorMessage);
                     }
                     //}
                 }
             }
         }
     }
 }
Example #3
0
 /**
  * Get a list of the subscribers to a mailing list.
  * 
  * @param int $listId The id of the list to get information about.
  * @param bool $useCache If true, use the cached result if available.
  * @return array List of subscriber information.
  */
 public function listSubscribers($listId, $useCache = true)
 {
     $cacheName = $this->__getCacheName(__FUNCTION__, array($listId));
     if ($useCache) {
         $cachedResult = $this->__getCachedResult($cacheName);
         if ($cachedResult != null) {
             return $cachedResult;
         }
     }
     $result = $this->__api->listMembers($listId, 'subscribed', null, 0, 5000);
     $this->__cacheResult($cacheName, $result);
     return $result;
 }
<?php

/**
This Example shows how to pull the Members of a List using the MCAPI.php 
class and do some basic error checking.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php';
//contains apikey
$api = new MCAPI($apikey);
$retval = $api->listMembers($listId, 'subscribed', null, 0, 5000);
if ($api->errorCode) {
    echo "Unable to load listMembers()!";
    echo "\n\tCode=" . $api->errorCode;
    echo "\n\tMsg=" . $api->errorMessage . "\n";
} else {
    echo "Members matched: " . $retval['total'] . "\n";
    echo "Members returned: " . sizeof($retval['data']) . "\n";
    foreach ($retval['data'] as $member) {
        echo $member['email'] . " - " . $member['timestamp'] . "\n";
    }
}
?>
 
        $api->listBatchSubscribe($list_id, $to_add, false, true);
        if (!empty($api->errorMessage)) {
            trigger_error("Mailchimp API Error calling listBatchSubscribe(): " . $api->errorMessage, E_USER_ERROR);
        }
    } else {
        // listBatchSubscribe doesn't always update all the merge vars correctly (perhaps if there's a case variation
        // in the email address) so when there's not too many we call listSubscribe individually
        foreach ($to_add as $add) {
            if (!$api->listSubscribe($list_id, $add['EMAIL'], $add, 'html', FALSE, TRUE, FALSE, false)) {
                trigger_error("listSubscribe returned false: " . $api->errorMessage);
            }
        }
    }
}
// Then, for all list members, check if they are in the report members list and if not add them to the "remove" list.
$list_members = $api->listMembers($list_id, 'subscribed', null, 0, 10000);
if (!empty($api->errorMessage)) {
    trigger_error("Mailchimp API Error calling listMembers(): " . $api->errorMessage, E_USER_ERROR);
}
$to_remove = array();
foreach ($list_members['data'] as $member) {
    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)) {
Example #6
0
     $get_address_id = tep_db_query("SELECT address_book_id FROM address_book WHERE customers_id = '" . (int) $_GET['cID'] . "' ORDER BY address_book_id DESC LIMIT 1");
     $address_id = tep_db_fetch_array($get_address_id);
     $new_address_id = $address_id['address_book_id'];
     tep_redirect(tep_href_link(FILENAME_CUSTOMERS, tep_get_all_get_params(array('cID', 'action', 'add_id')) . 'cID=' . $_GET['cID'] . '&action=edit&add_id=' . $new_address_id));
     break;
 case 'mailchimp':
     include_once DIR_WS_CLASSES . "MCAPI.class.php";
     $api = new MCAPI(MAILCHIMP_API);
     $list_id = MAILCHIMP_ID;
     if (MAILCHIMP_LAST_SYNC == '') {
         $last_update = null;
     } else {
         $last_update = MAILCHIMP_LAST_SYNC;
     }
     //Check for subscribed customers
     $retval = $api->listMembers($list_id, 'subscribed', $last_update, 0, 5000);
     if ($api->errorCode) {
         echo "Unable to load listMembers()!";
         echo "\n\tCode=" . $api->errorCode;
         echo "\n\tMsg=" . $api->errorMessage . "\n";
         echo "Members returned: " . sizeof($retval) . "\n";
     } else {
         $sub_list_size = sizeof($retval);
         foreach ($retval as $member) {
             // Now update local database to reflect mailchimp settings
             tep_db_query("update " . TABLE_CUSTOMERS . " set customers_newsletter = '1' where customers_email_address = '" . $member['email'] . "'");
         }
     }
     //Check for unsubscribed customers
     $retval = $api->listMembers($list_id, 'unsubscribed', $last_update, 0, 5000);
     if ($api->errorCode) {
Example #7
0
function mailchimp_hook_cron()
{
    $data = select_query("tblmailchimpconf", "name, value", array());
    while ($r = mysql_fetch_array($data)) {
        switch ($r['name']) {
            case "apikey":
                $apikey = $r['value'];
                break;
            case "clientfield":
                $clientfield = $r['value'];
                break;
        }
    }
    $api = new MCAPI($apikey);
    $lists = $api->lists();
    // Set everyone to unsubscribed
    $d = update_query("tblcustomfieldsvalues", array("value" => ""), array("fieldid" => $clientfield));
    foreach ($lists['data'] as $k => $v) {
        $listid = $v['id'];
        $listname = $v['name'];
        $listarr = $listid . "|" . $listname;
        $retval = $api->listMembers($listid, "subscribed", null, null, 15000);
        if ($api->errorCode) {
            $desc = $api->errorMessage;
            $query = "INSERT INTO tblactivitylog (date, description, user, userid) VALUES(now(), '{$desc}', 'System', '')";
            full_query($query);
        } else {
            foreach ($retval['data'] as $k => $v) {
                $email = $v['email'];
                $data = select_query("tblclients", "id", array("email" => $email));
                while ($res = mysql_fetch_array($data)) {
                    $clientid = $res['id'];
                    $cs = select_query("tblcustomfieldsvalues", "value", array("fieldid" => $clientfield, "relid" => $clientid));
                    if (mysql_num_rows($cs)) {
                        update_query("tblcustomfieldsvalues", array("value" => $listarr), array("fieldid" => $clientfield, "relid" => $clientid));
                    } else {
                        insert_query("tblcustomfieldsvalues", array("fieldid" => $clientfield, "relid" => $clientid, "value" => $listarr));
                    }
                }
            }
        }
    }
}
 public function CleanUpSubscriptionStatus($list = null)
 {
     if (!is_object($list)) {
         SS_Log::log("CleanUpSubscriptionStatus() Requireds MCList Object Parameter!", SS_Log::ERR);
         return false;
     }
     $emails = array();
     $api = new MCAPI($this->apikey);
     // Get ALL Subscribed List Members
     $retval = $api->listMembers($list->ListID, 'subscribed', null, 0, 5000);
     if ($api->errorCode) {
         SS_Log::log("API Call Failed: listMembers('" . $list->ListID . "', 'subscribed', null, 0, 5000); Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR);
     } else {
         SS_Log::log("API Call Success: listMembers('" . $list->ListID . "', 'subscribed', null, 0, 5000); Returned Members = " . $retval['total'], SS_Log::NOTICE);
         if ($retval['total'] > 0) {
             foreach ($retval['data'] as $member) {
                 $emails[] = strtolower($member['email']);
             }
         }
     }
     // Get ALL Unsubscribed List Members
     $retval = $api->listMembers($list->ListID, 'unsubscribed', null, 0, 5000);
     if ($api->errorCode) {
         SS_Log::log("API Call Failed: listMembers('" . $list->ListID . "', 'unsubscribed', null, 0, 5000); Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR);
     } else {
         SS_Log::log("API Call Success: listMembers('" . $list->ListID . "', 'unsubscribed', null, 0, 5000); Returned Members = " . $retval['total'], SS_Log::NOTICE);
         if ($retval['total'] > 0) {
             foreach ($retval['data'] as $member) {
                 $emails[] = strtolower($member['email']);
             }
         }
     }
     if (!empty($emails)) {
         // For All Subscription Records On The Webstie Which Arnt In The MailChimp List (MailChimp Admin Panel Deletions and Subscriptions Yet To Be Confirmed)
         $dl = new DataList("MCSubscription");
         $subs = $dl->where("\"MCListID\" = '" . $list->ID . "' AND LOWER(\"Email\") NOT IN (" . $this->arrayToCSV($emails) . ")");
         if (!empty($subs)) {
             foreach ($subs as $sub) {
                 $sub->setField("Subscribed", 0);
                 $sub->setSyncMailChimp(false);
                 $sub->write();
             }
         }
     }
 }