Monthly Archives: February 2015

How to Setup Your Own Mail Server that Will Deliver

There are many reasons why you might want to have your own SMTP server. But there are also many reasons why lots of businesses outsource email sending to third party services. One of the big problems with having your own SMTP server is that it is quite hard to set the whole thing up to get a solid delivery rate. What does it mean? It means that if you just naively install an SMTP server and try to send emails through it, many of your emails will not be delivered. Moreover, you will not even know that your emails did not reach their recipients. If you have decided that you want your own SMTP server, this article will help you reach a solid delivery rate – i.e. your emails will be delivered to existing mail boxes and will not get deleted as spam. If something is not configured perfectly, your emails can be lost completely without a notice, moved to Junk folders, or not accepted for a delivery. So, if you are asking following questions: How to deliver an email? How to setup MX server? How to increase the delivery rate? Just keep reading.

Note that the methods described in this article will work for you only if you are a legitimate business and do not intend to send spam. If you do send spam, it is a good thing that your emails are not delivered, and even if you set everything properly, many of your mails will get deleted.

This article only discusses sending emails. Receiving emails with your own mail server is another wide topic. We will use the term mail server universally for all roles and names such as mail exchanger, MX host, MTA (mail transfer agent), or mail relay.

Continue reading

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
//
// 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/information/privacy-policy-and-tos - ODT TOS
//  * 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.");