Re-subscribe a user that's previously unsubscribed (via the API)

Kane

Member
Is it possible to change the status of a subscriber on a specific list from 'Unsubscribed' to 'Confirmed', using the API?
 
Nope, but if you need it you can code the endpoint to do this yourself, shouldn't be that hard.
Additionally, you can try to add the subscriber once again if your list has single opt in enabled.
You could also fetch the subscriber, delete it, then add it again.
So you have plenty options :D
 
Thanks for your advice! The "delete" option seems to work.

But... if there's an autoresponder series that they've already received, they'll start receiving all those emails again. So I don't think that's a great option. We're not sure how to just "reactivate" the user on the same list, so they don't receive previously-sent autoresponder messages. Any ideas?
 
The above was all i had :D
I could add two api endpoints to confirm or unsubscribe certain subscriber...
 
I have implemented this:
PHP:
// UPDATE EXISTING SUBSCRIBER
$response = $endpoint->update('LIST-UNIQUE-ID', 'SUBSCRIBER-UNIQUE-ID', array(
    'EMAIL'    => 'john.doe@doe.com',
    'FNAME'    => 'John',
    'LNAME'    => 'Doe Updated',

    // optional
    'details'  => array(
        'ip_address' => '1.2.3.4',
        'status'     => 'confirmed', // confirmed | unconfirmed | unsubscribed
        'source'     => 'api', // api | web | import                
    ),
));
 
Darn, it doesn't seem to be working.

With the 'update' endpoint we can update every field except status :(
 
Ok, this might help:

We added:
$subscriber->status = ListSubscriber::STATUS_CONFIRMED;

Inside \apps\api\controllers\List_subscribersController.php => actionUpdate() (line 496)

And after calling the 'update' endpoint from our app, the status for that subscriber in the MailWizz customer front-end still shows Unsubscribed
 
Hi
i cant set unsubscribed status on create user by api, status always is setted to unconfirmed.

data:

PHP:
Array
(
[ID] => 23650
[ID_GENDER] => 1
[ID_DEFAULT_GROUP] => 2
[LASTNAME] => XXX
[FIRSTNAME] => XXXX
[BIRTHDAY] => 0000-00-00
[EMAIL] => m+test1@XXXX.pl
[IS_GUEST] => 1
[DATE_ADD] => 2015-09-02 11:53:02
[details] => Array
(
[ip_address] => '127.0.0.1'
[status] => 'unsubscribed'
[source] => 'api'
)
)

When i trying to update user case: user switch off subscription => api then switched on -> api, on status unconfirmed i think should be sent Subscribe Confirmation Email (with opt-in-double)

My suggestion in endpoint getSubscriber response body should have status and source
 
Last edited:
Not sure, but mailwizz already returns the subscriber status.
I have just added ip and source too now, so from next version you will get these too.
 
Hi,

with new version 1.3.5.8 still cannot set unsubscribed status when creating new user by api, status always is setted to unconfirmed.

Regards,
Mariusz
 
PHP:
class MailWizzJob
{
    //unique main list id
    CONST LIST_ID = 'XXXXX';
    CONST SOURCE = 'api';
    CONST STATUS_CONFIRMED = 'confirmed';
    CONST STATUS_UNCONFIRMED = 'unconfirmed';
    CONST STATUS_UNSUBSCRIBED = 'unsubscribed';

    public $list_fields = null;

    /**
     * Default action for Job object
     *
     * @param $job
     * @param $data
     */
    public function run(Job $job, $data)
    {
        echo "success default \n";
    }

    /**
     * @param Job $job
     * @param $data
     * @throws \Exception
     */
    public function createUpdate(Job $job, $data)
    {
        try {
            if (isset($data['object'])) {
                //print_r($data['object']);
                //set user subscription status if checked newsletter or not
                $options = [
                    'details' => array(
                        'ip_address' => \Tools::getRemoteAddr(),
                        'status' => ($data['object']['newsletter'] == 1) ? self::STATUS_UNCONFIRMED : self::STATUS_UNSUBSCRIBED,
                        'source' => self::SOURCE,
                    ),
                ];
                //get list fields and make one array based on remote fields
                $sendData = $this->prepareFields($data['object']);
                //merge optional data
                $sendData = array_merge($sendData, $options);
                print_r($sendData);
                $endpoint = new \MailWizzApi_Endpoint_ListSubscribers();
                //create profile
                $response = $endpoint->createUpdate(self::LIST_ID, $sendData);

                if (!$this->isSuccess($response)) {
                    throw new \Exception('MailWizzAPI.createUpdate: ' . $this->getResponseErrorMsg($response) . ' Job data: ' . print_r($data,
                            true));
                }
            } else {
                throw new  \Exception(__METHOD__ . ': object is empty!');
            }
        } catch (\Exception $e) {
            throw new  \Exception($e->getMessage());
        }
    }

    /**
     * @param $inputFields
     * @return array
     * @throws \Exception
     */
    public function prepareFields($inputFields)
    {
        //get list fields from api
        $this->getListFields();
        //make upper case and remove unknown fields from field list
        $sendFields = array_intersect_key(array_change_key_case($inputFields, CASE_UPPER), $this->list_fields);

        return $sendFields;
    }


    /**
     * Get list fields from api
     *
     * @return mixed
     * @throws \Exception
     */
    public function getListFields()
    {
        try {
            if (!$this->list_fields) {
                $endpoint = new \MailWizzApi_Endpoint_ListFields();
                $response = $endpoint->getFields(self::LIST_ID);
                if ($this->isSuccess($response)) {
                    $fields = $this->getResponseDataFieldValue($response, 'records');
                    $this->list_fields = [];
                    foreach ($fields as $field) {
                        $this->list_fields[$field['tag']] = '';
                    }
                    unset($endpoint);
                    unset($response);

                } else {
                    throw new \Exception('MailWizzAPI.getFields: ' . $this->getResponseErrorMsg($response));
                }
            }
        } catch (\Exception $e) {
            throw new  \Exception($e->getMessage());
        }
    }

    /**
     * @param $response
     * @return bool
     */
    protected function isSuccess($response)
    {
        return ($response->body['status'] == 'success') ? true : false;
    }

    /**
     * @param $response
     * @param $name
     * @return mixed
     * @throws \Exception
     */
    protected function getResponseDataFieldValue($response, $name)
    {
        if (isset($response->body['data'][$name])) {
            return $response->body['data'][$name];
        } else {
            throw new \Exception('getResponseDataFieldValue: ' . $name . ' is not set in response: ' . print_r($response,
                    true));
        }
    }

    /**
     * @param $response
     * @return string
     */
    protected function getResponseErrorMsg($response)
    {
        return ($response->body['status'] == 'error') ? $response->body['error'] : 'no error message';
    }

    /**
     * @param Job $job
     * @param $data
     * @throws \Exception
     */
    public function orderStats(Job $job, $data)
    {
        try {
            //print_r($data);
            if (isset($data['object'])) {
                $customer = new \Customer($data['object']['id_customer']);
                $data = array_merge($customer->getFields(), $customer->getStats());
                $data['id'] = $customer->id;
                $endpoint = new \MailWizzApi_Endpoint_ListSubscribers();
                //get list fields and make one array based on remote fields
                $sendData = $this->prepareFields($data);
                //create profile
                $response = $endpoint->createUpdate(self::LIST_ID, $sendData);

                if (!$this->isSuccess($response)) {
                    throw new \Exception('MailWizzAPI.orderStats: ' . $this->getResponseErrorMsg($response) . ' Job data: ' . print_r($data,
                            true));
                }

            } else {
                throw new  \Exception(__METHOD__ . ': object is empty!');
            }
        } catch (\Exception $e) {
            throw new  \Exception($e->getMessage());
        }
    }

    /**
     * @param Job $job
     * @param $data
     * @throws \Exception
     */
    public function delete(Job $job, $data)
    {
        try {
            if (isset($data['object'])) {
                $endpoint = new \MailWizzApi_Endpoint_ListSubscribers();
                if (isset($data['object']['email'])) {
                    $response = $endpoint->deleteByEmail(self::LIST_ID, $data['object']['email']);
                    if (!$this->isSuccess($response)) {
                        echo 'MailWizzAPI.delete: ' . $this->getResponseErrorMsg($response) . "\n";
                    }
                }
            } else {
                throw new  \Exception(__METHOD__ . ': object is empty!');
            }
        } catch (\Exception $e) {
            throw new  \Exception($e->getMessage());
        }
    }

    /**
     * Fallback action for Job object
     *
     * @param $data
     */
    public function failed($data)
    {
        echo __METHOD__ . "\n";
        echo 'Job Data: ' . print_r($data, true);
    }

PHP:
//data sent by api i got the same custom fields in list
[Mon, 07 Sep 2015 14:39:12 +0200] Queue:prestashop Job:219 ecbox\queue\jobs\MailWizzJob@createUpdate (4.27MB) Attempts:1 [1/50]
Array
(
[ID] => 1
[ID_GENDER] => 1
[ID_DEFAULT_GROUP] => 3
[LASTNAME] => XXXX
[FIRSTNAME] => XXXX
[BIRTHDAY] => 1975-8-7
[EMAIL] => lutek@XXXXX.pl
[IS_GUEST] => 0
[DATE_ADD] => 2009-12-12 21:41:39
[details] => Array
(
[ip_address] => 127.0.0.1
[status] => unsubscribed
[source] => api
)

)

another strange behavior i'm sending ip address 127.0.0.1 in database is saved my remote host ip.
 
i got the bug:

Code:
diff --git a/apps/api/controllers/List_subscribersController.php b/apps/api/controllers/List_subscribersController.php
index 5f38bc8..ebe99ce 100644
--- a/apps/api/controllers/List_subscribersController.php
+++ b/apps/api/controllers/List_subscribersController.php
@@ -347,7 +347,7 @@ class List_subscribersController extends Controller
         }

         // since 1.3.5.7
-        $details = (array)$request->getPut('details', array());
+        $details = (array)$request->getPost('details', array());
         if (!empty($details)) {
             if (!empty($details['status']) && in_array($details['status'], array_keys($subscriber->getStatusesList()))) {
                 $subscriber->status = $details['status'];
 
email confirmation is sent even with status unsubscribed for every new created user by api, is that right? List got double opt-in
 
Last edited:
My bad for this one, you are right, i wrongly fetched the details info in the create method. I have corrected the core code now.
 
Back
Top