Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check your old mailing list with the Mailboxlayer API and the results in a CSV file
<?php
set_time_limit(0);
ini_set('memory_limit', '1024M'); // should be enough for thousands of email addresses
$api_key = 'YOUR MAILBOXLAYER API KEY';
$fp = fopen('emails-checked.csv', 'w');
fputcsv($fp, array('Email address', 'Valid MX', 'Valid SMTP', 'Score'));
$count = 0;
if (($handle = fopen("emails-need-to-be-checked.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$ch = curl_init('https://apilayer.net/api/check?access_key='.$api_key.'&email='.$data[0]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$validationResult = json_decode($json, true);
$row = array($data[0], $validationResult['mx_found'], $validationResult['smtp_check'], $validationResult['score']);
fputcsv($fp, $row);
$count++;
if ($count % 4 == 1) sleep(1); // Don't reach the 300 requests/minute rate limit
}
}
fclose($fp);
@finalwebsites

This comment has been minimized.

Copy link
Owner Author

commented Apr 25, 2019

We use this snippet for mailing list which hasn't been user for more than 6 month. This way we keep the bounce rate for email campaigns very low.
Read our blog post about email address validation to learn more about the Mailboxlayer API.

Your emails-need-to-be-checked.csv file should contain only the email address (one for each row).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.