The FuzzyBlog! : PHP Code for Testing If an SMTP Address Exists The FuzzyBlog!

Marketing 101. Consulting 101. PHP Consulting. Random geeky stuff. I Blog Therefore I Am.

Home FuzzyGroup About Us Our Services

PHP Code for Testing If an SMTP Address Exists

This code taken from Mastering PHP 4.1 from Sybex. Chapter 10, Listing 10.7. 

  /*     * function email_works ($email)     * args: $email (address to test)     * We simulate an SMTP session to the server and use the 'RCPT TO'     * to see whether the mail server will accept mail for this user.     */

    function email_works ($email) {        // get the list of mail serves for the user's host/domain name        list ($addr, $hostname) = explode ("@", $email);        if (!getmxrr ($hostname, $mailhosts))            $mailhosts = array ($hostname); // if no MX records found                                            // simply try their hostname        $test_from = "me@domain.com"; // replace w/ your address        for ($i = 0; $i < count ($mailhosts); $i++)        {            // open a connection on port 25 to the recipient's mail server            $fp = fsockopen ($mailhosts[$i], 25);            if (!$fp)                continue;            $r = fgets($fp, 1024);            // talk to the mail server            fwrite ($fp, "HELO " . $_SERVER['SERVER_NAME'] . "\r\n");            $r = fgets ($fp, 1024);            if (!eregi("^250", $r))            {                fwrite ($fp, "QUIT\r\n");                fclose ($fp);                continue;            }            fwrite ($fp, "MAIL FROM: <" . $test_from . ">\r\n");            $r = fgets ($fp, 1024);            if (!eregi("^250", $r))             {                fwrite ($fp, "QUIT\r\n");                fclose ($fp);                continue;           }            fwrite ($fp, "RCPT TO: <" . $email . ">\r\n");            $r = fgets ($fp, 1024);            if (!eregi("^250", $r))            {                fwrite ($fp, "QUIT\r\n");                fclose ($fp);                continue;            }            // if we got here, we can send email from $test_from to $mail            fwrite ($fp, "QUIT\r\n");            $r = fgets($fp, 1024);            fclose ($fp);            return true;        }        return false;    }
 

This Page was last update: 4/6/2003; 3:14:00 AM

Copyright 2003 The FuzzyStuff

Theme Design by Bryan Bell

Click here to visit the Radio UserLand website. Subscribe to "The FuzzyBlog!" in Radio UserLand. Click to see the XML version of this web page. Click here to send an email to the editor of this weblog.