Can i change the functionality of the unsubscribe link to blacklist the recipient?

jhall95

Member
Currently using the [direct_unsubscribe_url] as an unsubscribe button but I would like it is if it would blacklist the recipient to prevent a campaign from a different user emailing them or some kind of accidental resubscribe
 

twisted1919

Administrator
Staff member
That's not possible without custom coding, you can add this code in apps/init-custom.php (create the file if missing):
Code:
<?php

Yii::app()->hooks->addAction('frontend_lists_after_track_campaign_unsubscribe', function($controller, CampaignTrackUnsubscribe $track) {
    $track->subscriber->addToBlacklist("Your message here...");
});
Please note I haven't tested this, so make sure you do.
 

Melihck

New Member
I tried but it didn't work for me :(

I tested by sending a test mail from a campaign by click to send test button, and used unsub link which defined as [UNSUBSCRIBE_URL].

As a result, my email is unsub from the list but didn't black listed.

I should've seen it on admin panel > email blacklist right?

I would be more than appreciate if we can solve it.

Lastly here is my edit in init-custom.php

1673622051611.png

thanks.
 

twisted1919

Administrator
Staff member
@Melihck - of course, because if you are not confirmed anymore you can't be added to the blacklist, my bad on this.
Please see updated code:

PHP:
Yii::app()->hooks->addAction('frontend_lists_after_track_campaign_unsubscribe', function($controller, CampaignTrackUnsubscribe $track) {
    $exists = EmailBlacklist::model()->findByAttributes(['email' => $track->subscriber->email]);
    if (!empty($exists)) {
        return;
    }
    
    $model = new EmailBlacklist();
    $model->email         = $track->subscriber->email;
    $model->subscriber_id = $track->subscriber_id;
    $model->reason        = 'Add your message here';
    $model->save(false);
    
    $track->subscriber->saveStatus(ListSubscriber::STATUS_BLACKLISTED);
});
 
Top