You are here

public function CampaignMonitor::subscribe in Campaign Monitor 8

Subscribe a user to a given list, with information entered. If the user is already subscribed to the list, her/his 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/CampaignMonitor.php, line 841
Implementation of the CampaignMonitor class, which is a wrapper class for Campaign Monitor v3 API. It's implemented as a Singleton class and instances are created using the account variables and the static function getConnector(). An example:.

Class

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(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
      return FALSE;
    }
    $this
      ->removeSubscriberFromCache($listId, $email);
    return TRUE;
  }
  return FALSE;
}