Before your would enthusiastically jump around in your room, I can assure you: I didn’t figure out a way to create an automatic way to clean a dirty, scraped or purchased list for bulletproof emailing.
But I’ll show you how to use Mautic API to connect to 2 different email cleaning services and build it in into your marketing automation workflow. And oh yes, you can download the scripts for yourself as well for free.
The plan is to create a segment with contacts, that has to be cleaned, start a campaign for them, call our API script from Mautic using webhooks, and push back the results to our contact’s record.
The Script: Email Oversight
I choose two of my favorite email cleaning services for this tutorial: Email Oversight and Zerobounce. They both doing a great job, and both have lightning-fast API access. Both services can verify an email just by using a single curl command. Ideal.
First you need to create a new list in EmailOversight, as each email address has to be connected to a list. You can do it in your account:
We will need the list ID, which can be found all over the place:
Email oversight API is super simple, all we need to do is to call the proper endpoint with our list ID and email address, that we want to verify:
https://api.emailoversight.com/api/EmailValidation?apitoken='.$apitoken.'&listid='.$list.'&email='.$email
Okay, so let’s build our script, that accepts a call from Mautic, calls Email Oversight, and reports back what it has found.
In order to store what our cleaner reported back, we need to create a new custom field in Mautic. I’ll call verification status.
Now we need to create a new php sile on our server that we will call emailcheck.php.
- Capture incoming data from Mautic:
We need to be able to capture by using the same fields as Mautic is passing to this script.
// capture data from Mautic webhook
if ($_POST['auth'] != $auth) {die ("Authentication failed!");};
$email = $_POST['email'];
$list = $_POST['list'];
if ($email == ''){echo 'Email required';die;};
if ($list == ''){echo 'List ID required';die;};
2. Call Email Oversight
Here we are passing the api key, list ID and our email address. Email Oversight will give us a nice json file with the results. For the sake of this tutorial we will just save if the email is okay or not.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.emailoversight.com/api/EmailValidation?apitoken='.$apitoken.'&listid='.$list.'&email='.$email,
CURLOPT_USERAGENT => 'Mautic cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// echo $resp;
// Close request to clear up some resources
curl_close($curl);
$verification_status = $json->Result;
Push results into Mautic
This is a simple API call back to Mautic, as you see we populate the verification_status field.
$curl = curl_init();
// Set some options - we are passing in a user agent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",
CURLOPT_USERAGENT => 'Mautic Connector',
CURLOPT_POST => 1,
// posting the payload
CURLOPT_POSTFIELDS => array(
'email' => $email,
'verification_status' => $verification_status
)
));
$resp = curl_exec($curl);
curl_close($curl);
Email oversight can give back various results for an email, it is not just ‘good’ or ‘bad’. Emails can have different shade of badness. This is an example report listing the different results for emails:
Zerobounce
With Zerobounce you would have an almost identical script. There are only 2 differences: how you talk to Zerobounce, and how it answers.
This is how you call the email verification API endpoint:
https://api.zerobounce.net/v2/validate?api_key='.$api_key.'&email='.urlencode($email).'&ip_address='.urlencode($IPToValidate);
The IP address is not mandatory, but you have to use the parameter. The response will come back as json, that you can easily decode:
// decode the json response
$json = json_decode($response, true);
$verification_status = $json['status'];
The rest is pretty much the same.
The Mautic part
Okay, so we have a way to capture data from Mautic, process that data in Emai lOversight or Zerobounce, now we need to create the Mautic workflow to utilize our new amazing script.
First we will create a segment for the emails we want to clean. You can import a bunch of emails with the ‘to verify’ tag, and filter for that:
Now let’s create our cmapaign, that does the whole workflow. Create a new campaign, and choose our freshly created segment as source. Than add a webhook as step 1:
If we set up everything right, this webhook will call our script, get a result, and use an API call to update the contact’s verification status.
The whole campaign looks like this:
Once you add emails to the segment, you’ll see the verification going through and your contacts verification status will be nicely populated:
Both scripts can be downloaded here for couple of days, than I’ll move them behind a registration wall.
Yahya
says:
Hello Joey,
Thank you for this tutorial and for sharing it with us.
I have to say that it took me some time to try to understand what you did, because there are a lot of things that I think need to be explained in detail for people who are beginners or who have only a basic knowledge of programming.
Anyway, I use another email validation service. But I was able to connect it with Mautic by customizing a php script similar to yours. I did some tests of validation of some emails and my txt file on the server returns the email and the response.
However, I didn’t understand how the verification_status clause is used inside Mautic and how you could get this status on Mautic, in your example. I have indeed built the same workflow as you with an output segment but I can’t find my response inside. If you could please detail how you set up the output segment and how I can separate the good emails from the bad ones on Mautic. By using tags or something like that?
Thanks in advance.
jos0405
says:
Hello Yahya, sorry for the late answer.
I cannot create tutorials for all programming levels, but I’m happy you figured it out 🙂
The verification status is a custom field in Mautic. Once you fill in the info needed you can start a campaign for a segment, where verification status is not empty. In this campaign you can delete if the status is bad, and confirm if it’s good.
How do you get this field filled out?
I used API in my example. The php script posts back an API call with the verification status inclided. The text file you get is just for logging and debugging purposes.
I hope that helps.