Appropriate return value when removing items with a filter

Discussion in 'Extensions' started by Souther, Nov 19, 2016.

  1. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    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?
     
  2. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,433
    Likes Received:
    2,426
    @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();
    });
    
     
  3. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    @twisted1919 Awesome. Thanks for the assist. I'm really enjoying learning the internals of what you have built here.
     
    twisted1919 likes this.
  4. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    @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.
     
  5. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    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();
     
  6. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    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.
     
  7. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,433
    Likes Received:
    2,426
    @Souther - use
    $deliveryLog->status = CampaignDeliveryLog::STATUS_SUCCESS; not
    CampaignDeliveryLog::STATUS_FATAL_ERROR;
     
  8. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    @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?
     
  9. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,433
    Likes Received:
    2,426
    @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.
     
    Souther likes this.
  10. Souther

    Souther New Member

    Joined:
    Oct 25, 2016
    Messages:
    23
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    @twisted1919 Thanks- This is exactly what I was looking for. I almost got there on my own but alas I was trying to set 'max_retries', not 'retries'.
     

Share This Page