ODT API – Bulk Email Verifier API Sample

The code sample below demonstrates how to use Online Domain Tools Email Verifier API. It uses asynchronous callback method in order to minimize needs for settings of the environment where the script can be successfully run. Any PHP 5.3+ hosting should be able to run this script. The script loads emails from the database and processes them in as many batches as needed, while respecting the limitations of ODT API.

PHP 5.3+ Sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
//
// This script is a sample on how to use Online Domain Tools Email Verifier API.
// It allows you to verify any amount of email addresses automatically despite the API
// limitation of a maximum of 1000 emails per request.
//
// This script is provided "AS IS" without warranties of any kind, either express or implied.
// Everyone is allowed to use it, copy it, or create derived products from it, completely
// free of charge, provided that Online Domain Tools TOS (see the link below) is respected
// and not violated.
//
//
// Copyright (c) 2015 - Online Domain Tools
//
// Relevant links:
//  * http://email-verifier.online-domain-tools.com/ - ODT Bulk Email Verifier
//  * http://online-domain-tools.com/information/api - ODT API specification
//  * http://online-domain-tools.com/ - Online Domain Tools homepage
//
//
// How does it work?
//  * This script loads input emails stored in $emailsInputFile, separated by newlines.
//  * It reads first $emailsPerRequest lines from the input file and sends them to EV ODT API
//    for verification.
//  * Asynchronous callback mode is used with ODT API so that this script is called by ODT
//    when the job is done.
//  * ODT uses POST request with JSON data to report the results to callback.
//  * The result is processed and good emails are written to $emailsValidatedFile.
//  * The processed emails are then removed from $emailsInputFile.
//  * The script logs its progress to $logFile, so in case of any problems, check the log
//    file.
//
// How to use it?
//  * You have to have ODT account with API enabled and obtain API key and secret. See PDF
//    ODT API specification and its section 1.
//  * Fill in your ODT API key and secret to $apiKey and $apiSecret global variables.
//  * Create a list of emails to verify and store them to maillist.txt, separated by
//    newlines.
//  * Copy maillist.txt and this script to the same folder on a PHP hosting server.
//  * Execute the script using your browser - simply access the script's URL.
//  * Wait until maillist.txt is empty or until maillist-log.txt does not grow.
//  * If maillist.txt is empty, the work is done. Otherwise, check maillist-log.txt to see
//    the problem.
//
 
# Settings
 
// Your API key & API secret
$apiKey = 'Your API key here'; // ODT-API-XXX
$apiSecret = 'Your API secret here';
 
// Is logging enabled
$loggingEnabled = true;
 
// Name of input file, in which emails are separated by newlines.
$emailsInputFile = __DIR__ . '/maillist.txt';
 
// Name of output file with valid emails separated by newlines.
// A valid email is is any email with EMAIL_VERIFIER_EMAIL_STATUS set to 2, 3, or 4.
$emailsValidatedFile = __DIR__ . '/maillist-verified.txt';
 
// Name of log file.
$logFile = __DIR__ . '/maillist-log.txt';
 
// URL of this script.
$thisUrl = "http" . (!empty($_SERVER['HTTPS'])?"s":"") . "://" . $_SERVER['SERVER_NAME']
         . $_SERVER['REQUEST_URI'];
 
// How many emails to process at once - note that 1000 is the maximum defined by API.
$emailsPerRequest = 100;
 
 
ini_set('display_errors', 1);
error_reporting(E_ALL);
 
 
/**
 * Appends $message to file $fileName.
 *
 * @param string $fileName Name of the file to append to.
 * @param string $data Data to append.
 */
function fileAppend($fileName, $data)
{
  $fp = fopen($fileName, "a");
 
  flock($fp, LOCK_EX);
  fwrite($fp, $data);
  flock($fp, LOCK_UN);
 
  fclose($fp);
}
 
 
/**
 * Appends $message to log file with time stamp.
 *
 * @param string $message
 */
function mlog($message)
{
  global $logFile, $loggingEnabled;
 
  if ($loggingEnabled)
  {
    $dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
    $time = $dateTime->format('Y-m-d H:i:s');
    $line = '[' . $time . '] ' . $message . "\n";
 
    fileAppend($logFile, $line);
  }
}
 
 
/**
 * Appends $message to output file.
 *
 * @param string $message
 */
function writeOutput($message)
{
  global $emailsValidatedFile;
 
  $line = $message . "\n";
  fileAppend($emailsValidatedFile, $line);
}
 
 
/**
 * @param string $key ODT API key.
 * @param string $secret ODT API secret.
 * @param string $action ODT API action - see ODT API documentation.
 * @param array $postData ODT API action data - see ODT API documentation.
 * @return array [bool $success, string $data] $success is true, if the operation succeeded,
                 in this case $data represents the returned result. If the operation failed,
                 $success is false and $data contains error message.
 */
function odtSendRequest($key, $secret, $action, array $postData)
{
  // generate POST data string
  $postFields = http_build_query($postData);
  $dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
  $time = $dateTime->format('Y-m-d H:i:s');
 
  $message = $key . $time . $postFields;
  $signature = hash_hmac('sha512', $message, $secret);
 
  // generate extra headers
  $headers = array(
    'Sign: ' . $signature,
    'Time: ' . $time,
    'Key: ' . $key
  );
 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, 'https://secured.online-domain-tools.com/api/user/'
                              . $action . '/');
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_TIMEOUT, 240);
 
  $data = curl_exec($ch);
 
  if ($data === false)
  {
    $error = curl_error($ch);
    $result = array(false, $error);
  } else
  {
    $result = array(true, $data);
  }
  curl_close($ch);
 
  return $result;
}
 
 
/**
 * This function is called during the inital request that starts the job.
 * It verifies that we have access rights to all files that we need.
 *
 * @return bool Returns true if succeeded, false otherwise.
 */
function initialization()
{
  mlog("initialization()");
 
  $result = false;
 
  global $emailsInputFile, $emailsValidatedFile, $logFile;
 
  $inputContents = file_get_contents($emailsInputFile);
  if ($inputContents !== false)
  {
    $res = file_put_contents($emailsInputFile, $inputContents);
    if ($res !== false)
    {
      // Clean output.
      $res = file_put_contents($emailsValidatedFile, "");
      if ($res !== false)
      {
        // Clean log file.
        $res = file_put_contents($logFile, "");
        if ($res !== false)
        {
          $result = true;
        }
        else mlog("initialization(): ERROR: Unable to write to log file '$logFile'.");
      }
      else mlog("initialization(): ERROR: Unable to write to output file "
              . "'$emailsValidatedFile'.");
    }
    else mlog("initialization(): ERROR: Unable to write to input file '$emailsInputFile'.");
  }
  else mlog("initialization(): ERROR: Unable to read input file '$emailsInputFile'.");
 
  mlog("initialization(-):" . (int)$result);
  return $result;
}
 
 
/**
 * Checks if there are any incoming data in form of JSON POST.
 * If not, does nothing and returns immediately. If yes, the data are processed.
 *
 * @return bool Returns true if succeeded, false otherwise.
 */
function processIncomingData()
{
  mlog("processIncomingData()");
 
  $result = false;
 
  // Load data from input.
  $json = file_get_contents('php://input');
  if ($json)
  {
    $jsonLen = strlen($json);
    mlog("processIncomingData(): Incoming data ($jsonLen bytes) detected "
       . "(first 256 bytes):\n" . substr($json, 0, 256) . "\n");
 
    $response = json_decode($json, true);
    if ($response !== null)
    {
      if ($response['success'] == 1)
      {
        mlog("processIncomingData(): Call succeeded.");
        $output = $response['output'];
        $emailsInfo = $output['emailsInfo'];
 
        $counter = 0;
        foreach ($emailsInfo as &$info)
        {
          mlog("processIncomingData(): Email " . $info['email'] . " status is "
             . $info['finalStatus'] . ".");
          if (($info['finalStatus'] == 2) || ($info['finalStatus'] == 3)
           || ($info['finalStatus'] == 4))
          {
            writeOutput($info['email']);
          }
          $counter++;
        }
 
        mlog("processIncomingData(): $counter emails processed.");
 
        $result = removeEmailsFromInputFile($counter);
      }
      else mlog("processIncomingData(): ERROR: Call failed with error message: '"
              . $response['message'] . "'");
    }
    else mlog("processIncomingData(): ERROR: Call failed. Server response is not a valid"
            . " JSON message.");
  }
  else
  {
    mlog("processIncomingData(): No incoming data. This is first call, perform file access"
       . " rights checks.");
    $result = initialization();
  }
 
  mlog("processIncomingData(-):" . (int)$result);
  return $result;
}
 
 
/**
 * Loads first $emailsPerRequest lines of input file.
 *
 * @return array Array of first $emailsInputFile emails from the input file.
 */
function loadEmailsFromInputFile()
{
  mlog("loadEmailsFromInputFile()");
 
  global $emailsInputFile, $emailsPerRequest;
   
  $result = array();
  $fp = fopen($emailsInputFile, "r");
  if ($fp)
  {
    $remain = $emailsPerRequest;
    while ((($line = fgets($fp)) !== false) && ($remain > 0))
    {
      $result[] = $line;
      $remain--;
    }
 
    fclose($fp);
  } else mlog("loadEmailsFromInputFile(): ERROR: Unable to open file '$emailsInputFile'.");
 
  mlog("loadEmailsFromInputFile(-)");
  return $result;
}
 
 
/**
 * Removes first $count emails from input file.
 *
 * @param int $count Number of emails to remove.
 * @return bool Returns true if succeeded, false otherwise.
 */
function removeEmailsFromInputFile($count)
{
  mlog("removeEmailsFromInputFile(count:$count)");
 
  global $emailsInputFile;
  $result = false;
 
  $lines = file($emailsInputFile, FILE_IGNORE_NEW_LINES);
  if ($lines !== false)
  {
    $newLines = array_slice($lines, $count);
    $newContent = implode("\n", $newLines);
    $res = file_put_contents($emailsInputFile, $newContent);
    if ($res !== false)
    {
      $result = true;
    } else mlog("removeEmailsFromInputFile(): ERROR: Unable to write to file"
              . " '$emailsInputFile'.");
  } else mlog("removeEmailsFromInputFile(): ERROR: Unable to read from file"
            . " '$emailsInputFile'.");
   
 
  mlog("removeEmailsFromInputFile(-):" . (int)$result);
  return $result;
}
 
 
// Return what is expected.
echo "ODT: OK";
 
 
// If processing is OK, continue.
if (processIncomingData())
{
  // Load and construct email list.
  $emailList = loadEmailsFromInputFile();
  $count = count($emailList);
  if ($count > 0)
  {
    $commaSeparatedList = implode(",", $emailList);
    
    
    // Send request via ODT API.
    list($isHttpRequestOk, $result) = odtSendRequest($apiKey, $apiSecret,
      "tool/email-verifier/check", array(
        "emails" => $commaSeparatedList,
        "asyncCallback" => $thisUrl
      ));
    
    if ($isHttpRequestOk)
    {
      $response = json_decode($result, true);
      if ($response !== null)
      {
        if ($response['success'] == 1)
        {
          mlog("API call succeeded, waiting for callback ...");      
        } else mlog("ERROR: API call failed with error: " . $response['message'] . ".");
      } else mlog("ERROR: API call failed. Server response is not a valid JSON message.");
    } else mlog("ERROR: HTTP request failed with error: " . $result . ".");
  } else mlog("No more emails to process, all done!");
}
else mlog("ERROR: Processing incoming data failed, stopping execution.");