Clean Your Emails Automatically with Mautic (2 Scripts)

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:'.$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.

  1. 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_URL => ''.$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

$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_URL => "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",
	  CURLOPT_USERAGENT => 'Mautic Connector',
	// posting the payload
		'email' => $email,
		'verification_status' => $verification_status
$resp = curl_exec($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:


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:'.$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.