public function massUnsubscribeAction() { $session = Mage::getSingleton('core/session'); Mage::helper('campaignmonitor')->log("massUnsubscribeAction"); $subscribersIds = $this->getRequest()->getParam('subscriber'); if (!is_array($subscribersIds)) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('newsletter')->__('Please select subscriber(s)')); $this->_redirect('*/*/index'); } else { try { if (Mage::helper('campaignmonitor')->isOAuth()) { $accessToken = Mage::getModel('campaignmonitor/auth')->getAccessToken(); $refreshToken = Mage::getModel('campaignmonitor/auth')->getRefreshToken(); $auth = array('access_token' => $accessToken, 'refresh_token' => $refreshToken); } else { $auth = Mage::helper('campaignmonitor')->getApiKey(); } $listID = Mage::helper('campaignmonitor')->getListId(); try { $client = new CS_REST_Subscribers($listID, $auth); } catch (Exception $e) { Mage::helper('campaignmonitor')->log("Error connecting to CampaignMonitor server: " . $e->getMessage()); $session->addException($e, $this->__('There was a problem with the subscription')); $this->_redirectReferer(); } foreach ($subscribersIds as $subscriberId) { $subscriber = Mage::getModel('newsletter/subscriber')->load($subscriberId); $email = $subscriber->getEmail(); Mage::helper('campaignmonitor')->log($this->__("Unsubscribing: %s", $email)); try { $result = $client->unsubscribe($email); if (!$result->was_successful()) { // If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { // Refresh the token Mage::helper('campaignmonitor')->refreshToken(); } // Make the call again $client->unsubscribe($email); } } catch (Exception $e) { Mage::helper('campaignmonitor')->log("Error in CampaignMonitor SOAP call: " . $e->getMessage()); } } } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } parent::massUnsubscribeAction(); }
<?php require_once '../../csrest_subscribers.php'; $wrap = new CS_REST_Subscribers('Your list ID', 'Your API Key'); $result = $wrap->unsubscribe('Email Address'); echo "Result of GET /api/v3/subscribers/{list id}/unsubscribe.{format}\n<br />"; if ($result->was_successful()) { echo "Unsubscribed with code " . $result->http_status_code; } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; }
public function indexAction() { $session = Mage::getSingleton('core/session'); // Don't do anything if we didn't get the email parameter if (isset($_GET['email'])) { $email = $_GET['email']; // Get the CampaignMonitor credentials if (Mage::helper('campaignmonitor')->isOAuth()) { $accessToken = Mage::getModel('campaignmonitor/auth')->getAccessToken(); $refreshToken = Mage::getModel('campaignmonitor/auth')->getRefreshToken(); $auth = array('access_token' => $accessToken, 'refresh_token' => $refreshToken); } else { $auth = Mage::helper('campaignmonitor')->getApiKey(); } $listID = Mage::helper('campaignmonitor')->getListId(); // Check that the email address actually is unsubscribed in Campaign Monitor. if ($auth && $listID) { // Retrieve the subscriber try { $client = new CS_REST_Subscribers($listID, $auth); $result = $client->get($email); if (!$result->was_successful()) { // If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { // Refresh the token Mage::helper('campaignmonitor')->refreshToken(); } // Make the call again $result = $client->get($email); } } catch (Exception $e) { Mage::helper('campaignmonitor')->log(sprintf("Error in SOAP call: %s", $e->getMessage())); $session->addException($e, $this->__('There was a problem with the unsubscription')); $this->_redirectReferer(); } // Get the subscription state $state = ""; try { if ($result->was_successful() && isset($result->response->State)) { $state = $result->response->State; } } catch (Exception $e) { Mage::helper('campaignmonitor')->log(sprintf("Error in SOAP call: %s", $e->getMessage())); $session->addException($e, $this->__('There was a problem with the unsubscription')); $this->_redirectReferer(); } // If we are unsubscribed, deleted or not subscribed in Campaign Monitor, mark us as // unsubscribed in Magento. if ($state != "Unsubscribed" && $state != "Not Subscribed" && $state != "Deleted") { try { $result = $client->unsubscribe($email); if (!$result->was_successful()) { // If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { // Refresh the token Mage::helper('campaignmonitor')->refreshToken(); } // Make the call again $result = $client->unsubscribe($email); } if ($result->was_successful()) { Mage::getModel('newsletter/subscriber')->loadByEmail($email)->unsubscribe(); Mage::getSingleton('customer/session')->addSuccess($this->__('You were successfully unsubscribed')); } } catch (Exception $e) { Mage::helper('campaignmonitor')->log(sprintf("%s", $e->getMessage())); Mage::getSingleton('customer/session')->addError($this->__('There was an error while saving your subscription details')); } } elseif ($state == "Unsubscribed" || $state == "Not Subscribed" || $state == "Deleted") { try { $subscriberStatus = Mage::getModel('newsletter/subscriber')->loadByEmail($email)->getStatus(); // 2 = Not Activated // 1 = Subscribed // 3 = Unsubscribed // 4 = Unconfirmed if ($subscriberStatus != 3) { Mage::getModel('newsletter/subscriber')->loadByEmail($email)->unsubscribe(); Mage::getSingleton('customer/session')->addSuccess($this->__('You were successfully unsubscribed')); $block = Mage::getModel('cms/block')->load('unsubscribe-custom-message'); if ($block) { Mage::getSingleton('customer/session')->addNotice($block->getContent()); } } else { Mage::getSingleton('customer/session')->addSuccess($this->__('You have already unsubscribed to our newsletter, click <a href="/subscribe">here</a> to resubscribe')); } } catch (Exception $e) { Mage::helper('campaignmonitor')->log(sprintf("%s", $e->getMessage())); Mage::getSingleton('customer/session')->addError($this->__('There was an error while saving your subscription details')); } } else { Mage::helper('campaignmonitor')->log($this->__("Not unsubscribing %s, not unsubscribed in Campaign Monitor", $email)); } } } $this->_redirect('/'); }
$output .= "},"; } //subs the last comma $output = substr($output, 0, -1); $output .= "]"; echo $output; } elseif ($req == "addSub" && isset($_GET["list_id"])) { //Add a new subscriber to the list $list_id = $_GET["list_id"]; //Save the data that were posted by the client via ajax $name = $_POST["Name"]; $email = $_POST["EmailAddress"]; //array manipiulation of the input parameters for the API method $subscriber = array("EmailAddress" => $email, "Name" => $name); //Get the appropriate wrapper to add a new subscriber $wrap_subscribers = new CS_REST_Subscribers($list_id, $auth); $add_subsciber = $wrap_subscribers->add($subscriber); } elseif ($req == "removSub" && isset($_GET["list_id"])) { //remove a subscriber $list_id = $_GET["list_id"]; //Email is needed as an input parameter for API method $email = $_POST["EmailAddress"]; //Get the appropriate wrapper to remove a subscriber $wrap_subscribers = new CS_REST_Subscribers($list_id, $auth); $remove_subscriber = $wrap_subscribers->unsubscribe($email); } else { echo "Please check the AJAX url"; } ?>
public function onUserBeforeDelete(Am_Event $event) { $user = $event->getUser(); $products = $user->getActiveProductIds(); if (!empty($products)) { return; } $list_ids = $this->getDi()->newsletterUserSubscriptionTable->getSubscribedIds($user->pk()); $lists = array(); foreach ($this->getDi()->newsletterListTable->loadIds($list_ids) as $list) { if ($list->plugin_id != $this->getId()) { continue; } $lists[] = $list->plugin_list_id; } if (empty($lists)) { return; } require_once 'lib/csrest_subscribers.php'; foreach ($lists as $listId) { $wrap = new CS_REST_Subscribers($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->get($user->email); if (!$result->was_successful()) { continue; } $result = $wrap->unsubscribe($user->email); if (!$result->was_successful()) { throw new Am_Exception_InternalError("Cannot unsubscribe user {$user->email} by reason: {$result->http_status_code}"); } } $this->debugLog("onUserBeforeDelete: user #{$user->pk()} was unsubscribed from lists (" . json_encode($lists) . ")"); }
/** * @param $observer */ public function customer_deleted($observer) { $event = $observer->getEvent(); $customer = $event->getCustomer(); if (Mage::helper('campaignmonitor')->isOAuth()) { $accessToken = Mage::getModel('campaignmonitor/auth')->getAccessToken(); $refreshToken = Mage::getModel('campaignmonitor/auth')->getRefreshToken(); $auth = array('access_token' => $accessToken, 'refresh_token' => $refreshToken); } else { $auth = Mage::helper('campaignmonitor')->getApiKey(); } $listID = Mage::helper('campaignmonitor')->getListId(); $email = $customer->getEmail(); if ($auth && $listID) { Mage::helper('campaignmonitor')->log("Customer deleted, unsubscribing: {$email}"); try { $client = new CS_REST_Subscribers($listID, $auth); $result = $client->unsubscribe($email); if (!$result->was_successful()) { // If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { // Refresh the token Mage::helper('campaignmonitor')->refreshToken(); } // Make the call again $client->unsubscribe($email); } } catch (Exception $e) { Mage::helper('campaignmonitor')->log("Error in SOAP call: " . $e->getMessage()); return; } } }