You are here

public function CampaignMonitor::subscribe in Campaign Monitor 7

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

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

Parameters

string $list_id: 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 $custom_fields: Optionally some custom fields that were defined in Campaign Monitor.

Return value

bool TRUE on success, FALSE otherwise.

File

lib/campaignmonitor.class.inc, line 927
Implementation of the CampaignMonitor class.

Class

CampaignMonitor
Implementation of the CampaignMonitor class.

Code

public function subscribe($list_id, $email, $name = '', array $custom_fields = []) {
  if ($obj = $this
    ->createSubscriberObj($list_id)) {
    $data = [
      'EmailAddress' => $email,
      'Name' => $name,
      'CustomFields' => $custom_fields,
      'ConsentToTrack' => 'yes',
      'Resubscribe' => TRUE,
    ];
    drupal_alter('campaignmonitor_subscriber_data', $data, $list_id);
    $result = $obj
      ->add($data);
    if (!$result
      ->was_successful()) {
      $this
        ->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
      return FALSE;
    }
    $this
      ->removeSubscriberFromCache($list_id, $email);
    return TRUE;
  }
  return FALSE;
}