We know why shoppers abandon carts and we also know that email remarketing is an effective way to lure some of those shoppers back to complete their purchase. But how can you try remarketing to users with abandoned carts without investing a lot of time and money?
Try a Simple PHP Remarketing Script
There are a lot of good reasons to implement a robust remarketing system but sometimes there’s beauty in simplicity, especially when you just want to try something out quickly to see how well it works. To that end, we present a simple PHP script to send remarketing emails to your shoppers when they abandon carts.
The Abandoned Carts Input File
This script reads a comma delimited text file that lists carts that are currently abandoned (your e-commerce software likely has a report that creates this report or you may need to bang out a quick SQL query to do it). The file contains one row for each abandoned cart with the first element of each row being the email address of the shopper the cart belongs to. After that comes the products that were left in the cart. For example:
shopper1@yahoo.com, Item1, Item2, Item3
shopper2@gmail.com, Item1
shopper3@hotmail.com, Item1, Item2
The Script That Uses the Input File to Send Email
The php script below then sends out a reminder email to each customer in the file that shows them which items are still in their carts. You can customize the from name and address, subject, and body of the email by editing the configuration section at the top of the script.
<?php
error_reporting(E_ALL ^ E_NOTICE);
////////////////////////////////
// Configuration
////////////////////////////////
$inputFilePath = "abandoned_carts.txt";
$delayBetweenEmails = 2; // in seconds
$from_email = "mystore@mystore.com";
$from_name = "MyStore";
$subject = "Don't let your cart expire!";
$messageBody =
"Hi there,
You left a few items in your cart last time you shopped with us:
{CART_CONTENTS}
Bad news! Your cart is about to expire! Now's your chance to snap up those awesome products. Just go to http://mystore.com/cart.php and finish your purchase in one easy step.
Thanks,
The MyStore team";
////////////////////////////////
// End of Configuration
////////////////////////////////
function getCSVValues($string, $separator=","){
$elements = explode($separator, $string);
for ($i = 0; $i < count($elements); $i++) {
$nquotes = substr_count($elements[$i], '"');
if ($nquotes %2 == 1) {
for ($j = $i+1; $j < count($elements); $j++) {
if (substr_count($elements[$j], '"') %2 == 1) { // Look for an odd-number of quotes
// Put the quoted string pieces back together again
array_splice($elements, $i, $j-$i+1,
implode($separator, array_slice($elements, $i, $j-$i+1)));
break;
}
}
}
if ($nquotes > 0) {
// Remove first and last quotes, then merge pairs of quotes
$qstr =& $elements[$i];
$qstr = substr_replace($qstr, ”, strpos($qstr, ‘”‘), 1);
$qstr = substr_replace($qstr, ”, strrpos($qstr, ‘”‘), 1);
$qstr = str_replace(’”"‘, ‘”‘, $qstr);
}
}
return $elements;
}
// Open the input file with all the abandoned cart info
$fh = fopen($inputFilePath, “r”);
if(!$fh){
echo “Can’t find input file.”;
exit(0);
}
// Iterate through each line of the file
$i = 0;
while($line = fgets($fh)){
$errors = array();
$message = $messageBody;
// Split the comma separated line into an array.
$cartContents = getCSVValues($line);
// First element of array is recipient email address
$recipient_email = array_shift($cartContents);
// Replace {CART_CONTENTS} in the message body with the cart’s contents
$cartContentsStr = “”;
foreach($cartContents as $cartItem) {
$cartContentsStr .= $cartItem . PHP_EOL;
}
$message = str_replace(”{CART_CONTENTS}”, $cartContentsStr, $message);
$headers = “From: $from_name <$from_email>“;
$headers .= PHP_EOL;
$headers .= “Reply-To: $from_email”;
mail($recipient_email,$subject,$message,$headers);
// Sleep for a bit so as not to overload the mail relay and get flagged
sleep($delayBetweenEmails);
$i++;
}
echo “$i emails successfully sent.”;
?>
To use this script, just copy and paste the above code into a file and save it as email_remarketing.php (or whatever) on your web server or a server with a php interpreter installed. Then add your abandoned carts report to the same location and call it abandoned_carts.txt. Browse to email_remarketing.php in a web browser and off it runs.
Some Caveats
There are a few things to keep in mind before using this script:
- This script was made to be run on linux servers. It will work on Windows servers but you have to add some mail relay info to the script (learn how here)
- This script sends only plain text email but you can tweak it to send HTML email if you like
- You shouldn’t send too many emails at a time with this script, otherwise you’ll run the risk of having the emails marked as spam and/or your mail relay throttling your throughput. Limit it’s use to a few hundred a day tops
- This is simple, simple, simple and doesn’t contain any real management or reporting functionality.
Using a simple php script to do email remarketing is not a sexy or glamorous solution to cart abandonment but it will let you test the waters quickly and see what kind of response it drives. If you like the results you see early on you can certainly add more functionality to the script or even consider building or buying a more advanced system.