Titan Fusion

Alexandar Tzanov's Personal Blog

Function: get_dnr_hostmaster


The get_dnr_hostmaster PHP function will return the hostmaster email address for a domain name.

Get_dnr_hostmaster works on the assumption that the supplied domain already exists and is hosted somewhere. Confirm by using the php function gethostbyname.

Source Code

  1. /* get_dnr_hostmaster
  2.  * Developer: Alexandar Tzanov
  3.  * Revised: 2013-08-05
  4.  * Version: 1.0
  5.  */
  6.  
  7. function get_dnr_hostmaster($domain = '')
  8. {
  9.     // Ensure that a value was provided.
  10.     if (!empty($domain))
  11.     {
  12.         // Prepare the CLI string and execute it.
  13.         $digup = "dig $domain SOA";
  14.         exec($digup, $NSResult);
  15.  
  16.         /* We are only interested in lien 11 of the output. It is the line with
  17.          * the answer to our question. We will split the line using tabs and
  18.          * then spaces, since those are the delimiters for the line.
  19.          *
  20.          * After the first split we need to remove any empty elements of the
  21.          * array. We also need to check if the $answerSection array has 4
  22.          * or 3 keys. This variation appear based on the domain name lenght.
  23.          */
  24.         $answerSection = explode("\t",$NSResult[11]);
  25.         $answerSection = array_values(array_filter($answerSection));
  26.         $answerSectionArrayKey = (array_key_exists(4, $answerSection)) ? 4 : 3;
  27.         $answerSection = explode(' ', $answerSection[$answerSectionArrayKey], 3);
  28.  
  29.         // Remove the trailing period
  30.         $unformatedEmailAddress = rtrim($answerSection[1], '.');
  31.  
  32.         /* Since there is no way to know the username convention, e.g. there is
  33.          * a period in it, we will assume that there isn't one. We will replace
  34.          * the first period in the string with an @ sign.
  35.          */
  36.         $emailAddress = preg_replace('/\./', '@', $unformatedEmailAddress, 1);
  37.  
  38.         return $emailAddress;
  39.     }
  40.     else
  41.     {
  42.         return 'WARNING: Missing domain name!';
  43.     }
  44. }

Usage

string get_dnr_hostmaster( string $domain )

Parameters

Name Definition Default Value
$domain The domain name for which to return the Hostmaster email address. N/A

Return

The get_dnr_hostmaster function will return the DNS record email address, e.g. user@example.com, ready to use. If no target domain is supplied a warning message will be returned.

Examples

$targetDomain = "sourcecodedna.org"; // Set the domain to check.
 
// Make sure the domain exists and is hosted.
if (gethostbyname($targetDomain) != $targetDomain)
{
    // Get the Hostmaster email address.
    $hostmasterEmailAddress = get_dnr_hostmaster($targetDomain);
 
    // Say hello to the Hostamster.
    @mail($hostmasterEmailAddress, '', "Hello World!\n");
}

Revision History

Date Version Changes
2013-08-05 01.00.00 Initial release.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.