Appropriate return value when removing items with a filter

Souther

New Member
I am tapping into the filter 'console_command_send_campaigns_before_send_to_subscriber' to call an external API. In some cases I want to remove the current email from the send process based on the API response.

What is the appropriate return value if I dont want the current emailParams to be turned into an email and sent? If I return false from my addFilter function it seems to retry sending on the next processing cron instead of just skipping the recipient. Should I just return a null object?
 

twisted1919

Administrator
Staff member
@Souther - In this case, in order to not retry sending, you have to log the delivery, so in your filter have something like:
Code:
$emailParams = Yii::app()->apps->addFilter('console_command_send_campaigns_before_send_to_subscriber', function(
$emailParams, $campaign, $subscriber, $customer, $server
){
     // your login start
     ...
     // your logic end 

     $deliveryLog = new CampaignDeliveryLog();
     $deliveryLog->campaign_id      = $campaign->campaign_id;
     $deliveryLog->subscriber_id    = $subscriber->subscriber_id;
     $deliveryLog->email_message_id = StringHelper::random(32);
     $deliveryLog->message          = "OK"
     $deliveryLog->status           = CampaignDeliveryLog::STATUS_SUCCESS;
    
     return array();
});
 

Souther

New Member
@Souther - In this case, in order to not retry sending, you have to log the delivery, so in your filter have something like:
Code:
$emailParams = Yii::app()->apps->addFilter('console_command_send_campaigns_before_send_to_subscriber', function(
$emailParams, $campaign, $subscriber, $customer, $server
){
     // your login start
     ...
     // your logic end

     $deliveryLog = new CampaignDeliveryLog();
     $deliveryLog->campaign_id      = $campaign->campaign_id;
     $deliveryLog->subscriber_id    = $subscriber->subscriber_id;
     $deliveryLog->email_message_id = StringHelper::random(32);
     $deliveryLog->message          = "OK";
     $deliveryLog->status           = CampaignDeliveryLog::STATUS_SUCCESS;
  
     return array();
});
@twisted1919 If I want to treat this similar to a fatal error (i.e. hard bounce), so my campaign send numbers are more accurate, can I use a status of CampaignDeliveryLog::STATUS_FATAL_ERROR instead?

This doesnt seem to work though. When trying this it seems to retry delivery still. Following the code it seems like it should have stopped after the first fatal error was received, as it would with a hard bounce.
 

Souther

New Member
Looks like a 'save' was missing from the original example. Here is my fixed code which seems to work...

Code:
                $deliveryLog = new CampaignDeliveryLog();
                $deliveryLog->campaign_id      = $campaign->campaign_id;
                $deliveryLog->subscriber_id    = $subscriber->subscriber_id;
                $deliveryLog->email_message_id = StringHelper::random(32);
                $deliveryLog->message          = "Recipient on Suppression List";
                $deliveryLog->status           = CampaignDeliveryLog::STATUS_FATAL_ERROR;
                $deliveryLog->save();

                return array();
 

Souther

New Member
Actually, my latest code seems to also add them to the system blacklist, just as a hard bounce would. I have tried
CampaignDeliveryLog::STATUS_GIVEUP but this just seems to get ignored and a retry is attempted 3 times anyways before actually giving up.

Am I doing something wrong? I would like to have it not retry but not blacklist the subscriber. You can think of the usecase here as similar to a campaign-specific suppression list capability.
 

Souther

New Member
@twisted1919 Thanks. I was attempting to cure the fact that this method produces an inaccurate count of messages sent in the campaign details/reports. STATUS_FATAL_ERROR blacklists the recipient from the system but produces an accurate report. Is there a Delivery Log status I can use which produces accurate campaign metrics but just stop delivery attempts and removes the recipient from processing from this campaign?
 

twisted1919

Administrator
Staff member
@Souther - this will do it for sure:
PHP:
                $deliveryLog = new CampaignDeliveryLog();
                $deliveryLog->retries += $deliveryLog->max_retries;
                $deliveryLog->campaign_id      = $campaign->campaign_id;
                $deliveryLog->subscriber_id    = $subscriber->subscriber_id;
                $deliveryLog->email_message_id = StringHelper::random(32);
                $deliveryLog->message          = "Recipient on Suppression List";
                $deliveryLog->status           = CampaignDeliveryLog::STATUS_TEMPORARY_ERROR;
                $deliveryLog->save();
Because right before the above model is saved, we have this code:
PHP:
if ($this->status == self::STATUS_TEMPORARY_ERROR) {
    $this->retries++;
    if ($this->retries >= $this->max_retries) {
        $this->status = self::STATUS_GIVEUP;
    }
}
So this will trick the app to think it has tried all the times and give up on sending and change the sttaus from temporary error to giveup.
 
Top