K
K
Konstantin2018-10-02 09:55:13
1C-Bitrix
Konstantin, 2018-10-02 09:55:13

Unsubscribing from Bitrix mailing list?

The documentation contains a description of the \Bitrix\Sender\Subscription
class , which has the subscribe and unsubscribe methods .
Actually, I figured out the subscribe method, but I can’t start unsubscribe in any way.
Here is the code that should, in theory, unsubscribe the user from the mailing list

if(CModule::IncludeModule("sender")){
    $params = array(
        'EMAIL' => "[email protected]", //EMAIL
        'UNSUBSCRIBE_LIST' => array(1), //Массив id рассылок
    );
    $unsubscribe = \Bitrix\Sender\Subscription::unsubscribe($params);
    die(json_encode($unsubscribe));
}

In response I receive the following message
Alias ​​"CONTACT_ID" matches already existing field "CONTACT_ID" of initial entity "\Bitrix\Sender\PostingRecipient". Please choose another name for alias.

How to make unsubscribe work correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Nikolaev, 2018-10-04
@gromdron

Based on the answer from Bitrix, as well as topics on the Bitrix forum , a small snippet of code for unsubscribing:

use \Bitrix\Main\Loader;
use \Bitrix\Sender;

/**
 * @var string Email пользователя
 */
$email = '[email protected]';

/**
 * @var string Список идентификаторов рассылок для отписки 
 */
$unSubscribeList = [
    3
];

try
{
    if ( !Loader::IncludeModule('sender') )
    {
        throw new \Exception("Sender module not installed");
    }

    if ( empty($unSubscribeList) )
    {
        throw new \Exception("Empty unSubscribeList param");
    }

    $contactId = Sender\ContactTable::addIfNotExist([
        'EMAIL' => $email
    ]);

    if ( !$contactId )
    {
        throw new \Exception("Contact by email not found and has error when creating");
    }

    $contact = new Sender\Entity\Contact($contactId);

    $data = $contact->getData();

    foreach ($data['SUB_LIST'] as $subscriptionId)
    {
        if ( in_array($subscriptionId, $unSubscribeList) )
        {
            $contact->unsubscribe($subscriptionId);
        }
    }

}
catch( \Exception $e )
{
    var_dump( $e->getMesage() );
}

A
Ainur Valiev, 2018-10-02
@vaajnur

gives an error message. try old api

$rsSub = CSubscription::GetByEmail($arParams["mail"]);
while ($arSub = $rsSub->Fetch())
{
$subscr->Update($arSub['ID'], array("ACTIVE" => "N", "SEND_CONFIRM" => "N"))
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question