7

PHP Code for Testing If an SMTP Address Exists

 3 years ago
source link: https://fuzzyblog.io/blog/story.radio.weblogs.com/2002/11/25/php-code-for-testing-if-an-smtp-address-exists.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP Code for Testing If an SMTP Address Exists

Nov 25, 2002

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.

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 = "[email protected]"; // 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


Posted In: #php


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK