You are here

function mailchimp_unsubscribe_process in Mailchimp 2.x

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_unsubscribe_process()
  2. 7.5 mailchimp.module \mailchimp_unsubscribe_process()
  3. 7.3 mailchimp.module \mailchimp_unsubscribe_process()
  4. 7.4 mailchimp.module \mailchimp_unsubscribe_process()

Unsubscribes a member from a Mailchimp list.

See also

Mailchimp_Lists::unsubscribe()

1 call to mailchimp_unsubscribe_process()
mailchimp_unsubscribe in ./mailchimp.module
Unsubscribes a member from a Mailchimp list.
1 string reference to 'mailchimp_unsubscribe_process'
mailchimp_unsubscribe in ./mailchimp.module
Unsubscribes a member from a Mailchimp list.

File

./mailchimp.module, line 800
Mailchimp module.

Code

function mailchimp_unsubscribe_process($list_id, $email) {
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new Exception('Cannot unsubscribe from list without Mailchimp API. Check API key has been entered.');
    }
    $mc_lists
      ->updateMember($list_id, $email, [
      'status' => 'unsubscribed',
    ]);
    \Drupal::moduleHandler()
      ->invokeAll('mailchimp_unsubscribe_success', [
      $list_id,
      $email,
    ]);

    // Clear user cache:
    mailchimp_cache_clear_member($list_id, $email);
    return TRUE;
  } catch (\Exception $e) {
    \Drupal::logger('mailchimp')
      ->error('An error occurred unsubscribing {email} from list {list}. "{message}"', [
      'email' => $email,
      'list' => $list_id,
      'message' => $e
        ->getMessage(),
    ]);
  }
  return FALSE;
}