Skip to content

Instantly share code, notes, and snippets.

@alexmchale
Created March 2, 2015 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexmchale/e91fd67df5ab829951db to your computer and use it in GitHub Desktop.
Save alexmchale/e91fd67df5ab829951db to your computer and use it in GitHub Desktop.
Special Sending Rule - Skip Duplicates Per Day
# This special sending rule will prevent the same email address from receiving
# more than one email per day from campaigns that are configured to use this
# rule.
#
# It has no affect on:
#
# (1) campaigns that don't use this SSR
# (2) autoresponders.
# We want "today" to be in the "America/Chicago" time zone.
date_default_timezone_set("America/Chicago");
# This function either:
#
# (1) grabs a previously allocated connection to a local Redis server
# (2) connects to the local Redis server
function GetGlobalRedis () {
global $globalRedis;
if ( is_null($globalRedis) ) {
$globalRedis = new Redis();
$globalRedis->connect("127.0.0.1", 6379);
}
return $globalRedis;
}
# This function does the following:
#
# (1) Test if the email has already received a message today.
# (2) Mark that this email has received a message today.
# (3) Expire the key tracking this email after 48 hours.
function HasEmailReceivedToday ($email) {
$redis = GetGlobalRedis();
$today = strftime("%Y-%m-%d");
$keyExpire = 60 * 60 * 48; // 48 hours, in seconds
$key = "HasEmailReceivedToday:$today:$email";
$didReceive = $redis->getset($key, "1") === "1";
$redis->expire($key, $keyExpire);
return $didReceive;
}
# This is the actual execution of the special sending rule. For each recipient,
# we want to skip it if it's already received an email today.
return array_map((function ($recipient) use ($campaign_information_hash) {
$email = $recipient->subscriber->email;
$didReceive = HasEmailReceivedToday($email);
$skip = $didReceive ? 1 : 0;
return array('skip' => $skip);
}), $multiple_recipients_information_hash);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment