Query MX records for a domain - without cache

Discussion in 'PHP' started by caratking, Aug 5, 2009.

  1. #1
    I would like to write some code that queries MX records for a domain, but I want to ensure they are not cached.

    Either getting them from the authoritative nameserver or somehow query a DNS server without caching.

    I am aware of getmxrr, but since it just does a call to whatever dns is defined then it will return cached results.

    Any suggestions?
     
    caratking, Aug 5, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    erm ttl is pretty low these days but yes, the benefits of cached mx are clearly visible when you re-run mx checks on long lists.

    the only way i see of doing this is:

    get NS info then do a dig @authoritative ns to get it from the horse's mouth, so to speak. of course, it would mean being on a unix host and being able to run shell commands... failing that, you are left with doing a socket connection to the NS. even so, this won't guarantee that propagation from the SOA has taken place over the web, could be that the zone file just got changed and the TTL is high...

    i am not sure if it's possible to bypass cache by params but I doubt it, the reply is a function of the OS and not PHP - PHP won't have any way of freshening the cache of the box or its respective NS.

    I wouldn't worry about it anyway, this filter you offer has a very limited scope - most spammers don't actually use their email address but cross link various valid emails from around the web, most with valid MX and open port 25.

    here's the function i wrote for my email verification purposes - checks compliant RFC, existing domain (well, a zone file anyway) and THEN mx (direct MX check when non-existing domain or missing zone file takes a lot longer to timeout). it can be extended to check port 25 on found MX as well, hope it helps :)

    usage: http://fragged.org/dev/emailPHPtest.php
    function checkEmail($email) {
        // checks if an email is composed correctly, if its domain exists and if it has valid MX records.
    
        // the following regex guarantees RFC compatibility with the largest and yet compliant / strict form of an email address.
        // check:
        //  - http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
        //  - http://fragged.org/dev/email_test.php - using this in javascript
        $validEmailRegex = '/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/';
    
        // default is valid.
        $check = true;
    
        if (!preg_match($validEmailRegex, $email)) {
            // if not composed correctly, has spaces etc this will trigger first.
            $check = "Sorry, this is not a valid email";
        }
        else {
            // composed ok, now get the domain:
            $parts = explode('@', $email);
    
            if (count($parts) > 0) {
                // get domain
                $dom = array_pop($parts);
    
                // check if it has any dns records, it's a fast check
                if (checkdnsrr($dom, "ANY")) {
    
                    // if so, perform the much slower MX check (especially so if the domain looks valid but has no data)
                    getmxrr($dom, $mxhosts);
    
                    // need at least 1 MX priority host
                    if (count($mxhosts) < 1)
                        $check = "$dom has no valid MX";
    
                    // you can now do a socket connection to port 25 of the MX hosts to see if the servers are up
                    // but this is excessive.
                }
                else {
                    $check = "$dom has no DNS records at all";
                }
    
            }
            else {
                // should not really come here if regex above works.
                $check = "Invalid / unknown domain.";
            }
        }
    
        return $check;
    } // end checkEmail
    
    PHP:
     
    Last edited: Aug 5, 2009
    dimitar christoff, Aug 5, 2009 IP