Hook for bounce processing?

Lakjin

Active Member
I use Dyn. For Dyn postback URL, is there an action I can hook into whenever an email is sent to the unsubscribe postback URL? I want to run some custom commands when the unsubscribe postback URL is triggered.
 

twisted1919

Administrator
Staff member
@Lakjin - the way web hooks processing is implemented allows you to define your own processing method.
From an extension, you can do this:
PHP:
Yii::app()->addFilter('dswh_process_map', function($map, $server, $controller){
    $map['dyn-web-api'] = function($server, $controller){
       
        $request    = Yii::app()->request;
        $event      = $request->getQuery('event');
        $bounceRule = $request->getQuery('rule', $request->getQuery('bouncerule')); // bounce rule
        $bounceType = $request->getQuery('type', $request->getQuery('bouncetype')); // bounce type
        $campaign   = $request->getQuery('campaign'); // campaign uid
        $subscriber = $request->getQuery('subscriber'); // subscriber uid

       // your bounce processing logic here. 
       // For details look in apps/frontend/controllers/DswhController.php at processDyn() method.
   }
   return $map;
});
 

Lakjin

Active Member
@Lakjin - the way web hooks processing is implemented allows you to define your own processing method.
From an extension, you can do this:
PHP:
Yii::app()->addFilter('dswh_process_map', function($map, $server, $controller){
    $map['dyn-web-api'] = function($server, $controller){
      
        $request    = Yii::app()->request;
        $event      = $request->getQuery('event');
        $bounceRule = $request->getQuery('rule', $request->getQuery('bouncerule')); // bounce rule
        $bounceType = $request->getQuery('type', $request->getQuery('bouncetype')); // bounce type
        $campaign   = $request->getQuery('campaign'); // campaign uid
        $subscriber = $request->getQuery('subscriber'); // subscriber uid

       // your bounce processing logic here.
       // For details look in apps/frontend/controllers/DswhController.php at processDyn() method.
   }
   return $map;
});
Great! So this would override processDyn? Could you point me in the right direction to learn how to create extensions for MailWizz?
 

Lakjin

Active Member
@Lakjin - the way web hooks processing is implemented allows you to define your own processing method.
From an extension, you can do this:

Just to make sure I did it properly, can you please confirm this is correct:
PHP:
class ExampleExt extends ExtensionInit
{
    public $name = 'Example';

    public $description = 'Changes to MailWizz';

    public $version = '1.0';

    public $minAppVersion = '1.3.6.2';

    public $author = 'Bob';

    public $website = 'http://example.com/';

    public $email = 'admin@example.com';

    public $allowedApps = array('*');
  
    public $notAllowedApps = array();

    public $cliEnabled = true;

    protected $_canBeDeleted = false;

    protected $_canBeDisabled = true;

    public function run()
    {
        Yii::app()->addFilter('dswh_process_map', function($map, $server, $controller){
            $map['dyn-web-api'] = function($server, $controller){
                $request    = Yii::app()->request;
                $event      = $request->getQuery('event');
                $bounceRule = $request->getQuery('rule', $request->getQuery('bouncerule')); // bounce rule
                $bounceType = $request->getQuery('type', $request->getQuery('bouncetype')); // bounce type
                $campaign   = $request->getQuery('campaign'); // campaign uid
                $subscriber = $request->getQuery('subscriber'); // subscriber uid

                $allowedEvents = array('bounce', 'complaint', 'unsubscribe');
                if (!in_array($event, $allowedEvents)) {
                    Yii::app()->end();
                }

                $campaign = Campaign::model()->findByUid($campaign);
                if (empty($campaign)) {
                    Yii::app()->end();
                }

                $subscriber = ListSubscriber::model()->findByAttributes(array(
                    'list_id'          => $campaign->list_id,
                    'subscriber_uid'   => $subscriber,
                    'status'           => ListSubscriber::STATUS_CONFIRMED,
                ));

                if (empty($subscriber)) {
                    Yii::app()->end();
                }

                $bounceLog = CampaignBounceLog::model()->findByAttributes(array(
                    'campaign_id'   => $campaign->campaign_id,
                    'subscriber_id' => $subscriber->subscriber_id,
                ));

                if (!empty($bounceLog)) {
                    Yii::app()->end();
                }

                if ($event == 'bounce') {
                    $bounceLog = new CampaignBounceLog();
                    $bounceLog->campaign_id     = $campaign->campaign_id;
                    $bounceLog->subscriber_id   = $subscriber->subscriber_id;
                    $bounceLog->message         = $bounceRule;
                    $bounceLog->bounce_type     = $bounceType == 'soft' ? CampaignBounceLog::BOUNCE_SOFT : CampaignBounceLog::BOUNCE_HARD;
                    $bounceLog->save();

                    if ($bounceLog->bounce_type == CampaignBounceLog::BOUNCE_HARD) {
                        $subscriber->addToBlacklist($bounceLog->message);
                    }
                    Yii::app()->end();
                }
              
                if ($event == 'complaint') {
                    $bounceLog = new CampaignBounceLog();
                    $bounceLog->campaign_id     = $campaign->campaign_id;
                    $bounceLog->subscriber_id   = $subscriber->subscriber_id;
                    $bounceLog->message         = 'complained';
                    $bounceLog->bounce_type     = CampaignBounceLog::BOUNCE_HARD;
                    $bounceLog->save();

                    if ($bounceLog->bounce_type == CampaignBounceLog::BOUNCE_HARD) {
                        $subscriber->addToBlacklist($bounceLog->message);
                    }
                    Yii::app()->end();
                }

                if ($event == 'unsubscribe') {
                    if (Yii::app()->options->get('system.cron.process_feedback_loop_servers.subscriber_action', 'unsubscribe') == 'delete') {
                        $subscriber->delete();
                        Yii::app()->end();
                    }

                    $subscriber->saveStatus(ListSubscriber::STATUS_UNSUBSCRIBED);

                    $trackUnsubscribe = CampaignTrackUnsubscribe::model()->findByAttributes(array(
                        'campaign_id'   => $campaign->campaign_id,
                        'subscriber_id' => $subscriber->subscriber_id,
                    ));

                    if (!empty($trackUnsubscribe)) {
                        Yii::app()->end();
                    }

                    $trackUnsubscribe = new CampaignTrackUnsubscribe();
                    $trackUnsubscribe->campaign_id   = $campaign->campaign_id;
                    $trackUnsubscribe->subscriber_id = $subscriber->subscriber_id;
                    $trackUnsubscribe->note          = 'Unsubscribed via Web Hook!';
                    $trackUnsubscribe->save(false);
                  
                    $email = $request->getQuery('email');
                      
                    $dynAPI = 'xyz';

                    $ch = curl_init( 'https://api.email.dynect.net/rest/json/suppressions/activate?apikey='.$dynAPI.'&emailaddress='.urlencode($email) );
                    $options = array(
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_SSL_VERIFYHOST => 0,
                        CURLOPT_SSL_VERIFYPEER => 0,
                        CURLOPT_HTTPHEADER => array('Content-type: application/json')
                    );
                    curl_setopt_array( $ch, $options );
                    $retJSON = curl_exec($ch);

                    Yii::app()->end();
                }

                Yii::app()->end();
          }
          return $map;
        });
    }
}

My biggest question is about
PHP:
Yii::app()->end();
-- is that OK in an extension, as I see it must return $map;?
 
Top