You are here

public function CampaignMonitorManager::subscribe in Campaign Monitor 8.2

Subscribe a user to a given list, with information entered.

If the user is already subscribed to the list, information will be updated with the new values.

Parameters

string $listId: The unique Campaign Monitor list ID.

string $email: The e-mail address that identifies the user.

string $name: Optionally the name of the user.

array $customFields: Optionally some custom fields that were defined in Campaign Monitor.

Return value

bool TRUE on success, FALSE otherwise.

File

src/CampaignMonitorManager.php, line 1000

Class

CampaignMonitorManager
Manager for Campaignmonitor.

Namespace

Drupal\campaignmonitor

Code

public function subscribe($listId, $email, $name = '', array $customFields = []) {
  if ($obj = $this
    ->createSubscriberObj($listId)) {
    $result = $obj
      ->add([
      'EmailAddress' => $email,
      'Name' => $name,
      'CustomFields' => $customFields,
      'Resubscribe' => TRUE,
    ]);
    if (!$result
      ->was_successful()) {
      $this
        ->addError($result->response->Message, $result->http_status_code);
      return FALSE;
    }
    $this
      ->removeSubscriberFromCache($listId, $email);
    return TRUE;
  }
  return FALSE;
}