Documentation

Add

Description and parameters

int
CSubscription::Add(
    array arFields,
    string SITE_ID = SITE_ID
 );

Method adds a newsletter subscription and sends to subscriber an email containing a subscription confirmation code (if not restricted). Non-static method.

Parameters

ParameterDescription Available from version
arFields Array with values for Subscription object fields. Additionally, the following fields can be defined:
  • RUB_ID - array with newsletter IDs this address has a subscription;
  • SEND_CONFIRM - confirmation to send a confirmation code email to a subscriber (Y/N).
  • ALL_SITES - unsubscribe a user from all site newsletters or only from a specified SITE_ID (Y/N).
  • USER_ID - optional. Registered user ID.
SITE_ID Site ID that defines template for subscription confirmation email. By default, parameter receives the value of current site.

Requirements to fields, passed to arFields

  • EMAIL - must contain a correct email address (see function check_email). Also, checks if EMAIL is unique.

Returned value

On success, returns a subscription ID. Otherwise, returns false and variable LAST_ERROR contains an error message (and throws exception CMain::ThrowException).

Example

//there must be at least one newsletter category
if(!is_array($RUB_ID) || count($RUB_ID) == 0)
    $strWarning .= "There must be at least one category."."<br>";

if($strWarning == "")
{
    $arFields = Array(
        "USER_ID" => ($USER->IsAuthorized()? $USER->GetID():false),
        "FORMAT" => ($FORMAT <> "html"? "text":"html"),
        "EMAIL" => $EMAIL,
        "ACTIVE" => "Y",
        "RUB_ID" => $RUB_ID
    );
    $subscr = new CSubscription;

    //can add without authorization
    $ID = $subscr->Add($arFields);
    if($ID>0)
        CSubscription::Authorize($ID);
    else
        $strWarning .= "Error adding subscription: ".$subscr->LAST_ERROR."<br>";
}


© «Bitrix24», 2001-2024