Example #1
0
 /**
  * Unsubscribes an e-mail address from CampaignMonitor and our database
  *
  * @param string        $email   The e-mail address to unsubscribe.
  * @param string $groupId The id of the group to unsubscribe from.
  * @return bool
  */
 public static function unsubscribe($email, $groupId = null)
 {
     // get objects
     $db = FrontendModel::getContainer()->get('database');
     $cm = self::getCM();
     // set group ID
     $groupId = !empty($groupId) ? $groupId : FrontendMailmotorModel::getDefaultGroupID();
     // get group CM ID
     $groupCMId = self::getCampaignMonitorID('list', $groupId);
     // group exists
     if (FrontendMailmotorModel::existsGroup($groupId)) {
         try {
             // unsubscribe the email from this group
             $cm->unsubscribe($email, $groupCMId);
         } catch (\Exception $e) {
             // for the unsubscribe function we ignore any errors
             // stop here if something went wrong with CM
             return false;
         }
         // set variables
         $subscriber['status'] = 'unsubscribed';
         $subscriber['unsubscribed_on'] = FrontendModel::getUTCDate('Y-m-d H:i:s');
         // unsubscribe the user
         $db->update('mailmotor_addresses_groups', $subscriber, 'email = ? AND group_id = ?', array($email, $groupId));
         // user unsubscribed
         return true;
     }
     // user not unsubscribed
     return false;
 }