You are here

public function MailchimpLists::getListsForEmail in Mailchimp 8

Gets all lists an email address is subscribed to.

Parameters

string $email: The email address to get lists for.

Return value

array Array of subscribed list objects.

Throws

MailchimpAPIException

File

lib/mailchimp-api-php/src/MailchimpLists.php, line 593

Class

MailchimpLists
Mailchimp Lists/Audiences library.

Namespace

Mailchimp

Code

public function getListsForEmail($email) {
  $list_data = $this
    ->getLists();
  $subscribed_lists = [];

  // Check each list for a subscriber matching the email address.
  if ($list_data->total_items > 0) {
    foreach ($list_data->lists as $list) {
      try {
        $member_data = $this
          ->getMemberInfo($list->id, $email);
        if (isset($member_data->id)) {
          $subscribed_lists[] = $list;
        }
      } catch (MailchimpAPIException $e) {
        if ($e
          ->getCode() !== 404) {

          // 404 indicates the email address is not subscribed to this list
          // and can be safely ignored. Surface all other exceptions.
          throw new MailchimpAPIException($e
            ->getMessage(), $e
            ->getCode(), $e);
        }
      }
    }
  }
  return $subscribed_lists;
}