Link to home
Start Free TrialLog in
Avatar of submissiontechnology
submissiontechnology

asked on

php getmxrr failing

For some reason getmxrr seems to be failing about 60% of the time, even on valid domains such as hotmail.com. Anyone have any ideas why this might be? The code I'm using is:

if (getmxrr($domain,$hosts)) {
 // send email
 echo "sent email to $domain\n";
}
else {
 echo "failed to find mx record for $domain\n";
}

The output as an example looks like this:

sent email to hotmail.com
sent email to gmail.com
failed to find mx record for hotmail.com
failed to find mx record for tiscali.co.uk
failed to find mx record for hotmail.com
sent email to hotmail.co.uk
failed to find mx record for hotmail.com
failed to find mx record for gmail.com
failed to find mx record for hotmail.co.uk
failed to find mx record for aol.com
sent email to hotmail.com
sent email to googlemail.com
sent email to yahoo.co.uk
sent email to gmail.com
sent email to yahoo.com
failed to find mx record for yahoo.com
failed to find mx record for hotmail.co.uk
sent email to blueyonder.co.uk
failed to find mx record for btinternet.com
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I assume you are NOT on windows as windows PHP doesn't have a getmxrr() function.

But I have a replacement function for windows that uses a program supplied with windows called nslookup.

I see from wikipedia that this program exists in *ix also, so can you try ...


function getmxrr($s_HostName, array &$a_MXHosts = NULL, array &$a_Weights = NULL)
      {
      // Simulate all the required network activity by executing windows' NSLOOKUP.
      $s_NSLookup = shell_exec("nslookup -q=mx {$s_HostName} 2>nul");
      preg_match_all("'^.*MX preference = (\d{1,10}), mail exchanger = (.*)$'simU", $s_NSLookup, $a_MXMatches);

      // If there is something to return ...
      if (count($a_MXMatches[2]) > 0)
            {
            // Produce output arrays if they have been requested.
            $i_ArgCount = func_num_args();
            if ($i_ArgCount > 1)
                  {
                  array_multisort($a_MXMatches[1], $a_MXMatches[2]);
                  switch ($i_ArgCount)
                        {
                        case 3 :
                              $a_Weights = $a_MXMatches[1];
                        case 2 :
                              $a_MXHosts = $a_MXMatches[2];
                        }
                  }
            return True;
            }
      else
            {
            return False;
            }
      }

You'll need to rename this function for *ix.

Ideally, assuming nslookup is found correctly, this should work. I don't know if you need to add .exe to the program name (I don't in windows).

If the results are different (i.e. nslookup always gets the mx records), then this could be a bug in PHP.
Avatar of submissiontechnology
submissiontechnology

ASKER

trim did the trick thank you hernst
Hernst24, how did you guess this?
I tried a list of domains and it work for all the domains (even the duplicates) and then I just added a whitespace to one entry which worked before (only god knows why I had that idea ;-) ) and in that case it failed.