WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A new version of the validateEmail function. Email verification based on a negative issue.
Categories : PHP, Email Update Picture
Pascal Chambenoit
Date : Jun 17th 1999
Grade : 3 of 5 (graded 7 times)
Viewed : 11798
File : validateEmail.php3
Images : No Images for this code example.
Search : More code by Pascal Chambenoit
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
/*
Originally
By: Jon S. Stevens jon@clearink.com
Copyright 1998 Jon S. Stevens, Clear Ink
This code has all the normal disclaimers.
It is free for any use, just keep the credits intact.

Enhancements and modifications:

By: Shane Y. Gibson shane@tuna.org
Organization: The Unix Network Archives (http://www.tuna.org./)
Date: November 16th, 1998
Changes: Added **all** comments, as original code lacked them.
Added some return codes to include a bit more description
for useability.

By : berber
Organization : webdev.berber.co.il
Date : April 10th, 1999
Changes : The script now handls all kind of domains (not only @xxx.yyy) as before.
Added a debuging mode which also works as a verbose mode.

By : Pascal Chambenoit
Organization : Techno-Prime, France
Date : June 16th, 1999
Changes : The script now execute a "negative" check of the email address.
As backup servers allways respond by a 250.. user known,
if a MX tell us that a user is not known, perhaps can we trust it.
Also added a \r to all fputs because some servers doesnt understand \n.
However, there are a few servers correctly configured on Earth... :-)
*/

/* This function takes in an email address (say 'shane@tuna.org')
* and tests to see if it's a valid email address.
*
* An array with the results is passed back to the caller.
*
* Possible result codes for the array items are:
*
* Item 0: [true|false] true for valid email address
* false for NON-valid email address
*
* Item 1: [SMTP Code] if a valid MX mail server found, then
* fill this array in with failed SMTP
* reply codes
*
* Item 2: [true|false] true for valid mail server found for
* host/domain
* false if no valid mail server found
*
* Item 3: [MX server] if a valid MX host was found and
* connected to then fill in this item
* with the MX server hostname
*
* EXAMPLE code for use is at the very end of this function.
*/

function validateEmail ($email)
{
$debug_=1;
// used for SMTP HELO argument
global $SERVER_NAME;

// initialize our return array, populating with default values
$return=array(false,"","","");

// assign our user part and domain parts respectively to seperate
// variables
list($user,$domain)=split("@",$email,2);
if($debug_==1) {
        echo"user: $user<BR>";
        echo"domain: $domain<BR>";
}
// split up the domain name into sub-parts
$arr=explode(".",$domain);

// figure out how many parts to the host/domain name portion there are
$count=count($arr);

// get our Top-Level Domain portion (i.e. foobar.org)
$tld=$arr[$count-2].".".$arr[$count-1];

// check that an MX record exists for Top-Level Domain, and if so
// start our email address checking
if (checkdnsrr($domain,"MX"))
{
        if($debug_==1) {
                 echo"Check DNS RR OK<BR>";
        }
// Okay...valid dns reverse record; test that MX record for
// host exists, and then fill the 'mxhosts' and 'weight'
// arrays with the correct information
//
if (getmxrr($domain,$mxhosts,$weight))
{
if($debug_==1) {
                                echo "MX LOOKUP RESULTS :<BR>";
                                for ( $i = 0; $i < count($mxhosts); $i++ ) {
                                        echo "      o $weight[$i] $mxhosts[$i]<BR>";
                                }
                                echo "<b>".count($mxhosts)." mail-servers found for this domain</b><BR>";

                }

         // sift through the 'mxhosts' connecting to each host
for ( $i=0; $i < count($mxhosts); $i++)
{
// open socket on port 25 to mxhosts, setting
                                // returned file pointer to the variable 'fp'
$fp = fsockopen ($mxhosts[$i], 25 );

// if the 'fp' was set, then goto work
if ($fp)
{
                                 if($debug_==1) {
                                 echo"<BR><BR><h2>$mxhosts[$i]</h2>";
                                        echo"Socket Opened successfully...<BR>";
                                         }
// work variables
$s = 0;
$c = 0;
$out = "";
// set our created socket for 'fp' to
// non-blocking mode
// so our fgets() calls will return
// right away
set_socket_blocking ( $fp, false );

// as long as our 'out' variable has a
// null value ("")
// keep looping (do) until we get
// something
//
do
{
// output of the stream assigned
// to 'out' variable
$out = fgets ( $fp, 2500 );
                                                if($debug_==1) {
                                                 if($out != "") echo"out: $out<BR>";
                                                }
// if we get an "220" code (service ready code (i.e greeting))
// increment our work (code (c)) variable, and null
// out our output variable for a later loop test
//
if ( ereg ( "^220", $out ) )
{
                                                 if($debug_==1) {
                                                        echo"Service ready on recipient machine.<BR>";
                                                 }
$s = 0;
$out = "";
$c++;
$return[2] = true;
$return[3] = $mxhosts[$i];
}
// elseif c is greater than 0
// and 'out' is null (""),
// we got a code back from some
// server, and we've passed
// through this loop at least
// once
//
else if (($c > 0) && ($out == ""))
{
        $return[2] = true;
break;
}

// else increment our 's'
// counter
else
{ $s++; }

// and if 's' is 9999, break, to
// keep from looping
// infinetly
if ( $s == 9999 ) {
                                                 if($debug_==1) {
echo"Reached maximum 10000 loops, breaking.<BR>";
}        
                                         break;
                                                }

} while ( $out == "" );

// reset our file pointer to blocking
// mode, so we wait
// for communication to finish before
// moving on...
set_socket_blocking ( $fp, true );

// talk to the MX mail server,
// validating ourself (HELO)
fputs ( $fp, "HELO ".$SERVER_NAME."\r\n");
                                  if($debug_==1) {
        echo"<BR>HELO $SERVER_NAME<BR>";
}
// get the mail servers reply, assign to
// 'output' (ignored)
$output = fgets ( $fp, 2000 );
                                  if($debug_==1) {
                                                echo"output : $output<BR>";
                                  }
                  // give a bogus "MAIL FROM:" header to
// the server
fputs ($fp,"MAIL FROM: <info@".$domain.">\r\n");
                                  if($debug_==1) {
        echo"MAIL FROM: <info@".$domain."><BR>";
}
// get output again (ignored)
$output = fgets ( $fp, 2000 );
                                  if($debug_==1) {
                                                echo"output : $output<BR>";
                                  }
// give RCPT TO: header for the email
// address we are testing
fputs($fp,"RCPT TO: <".$email.">\r\n");
                                 if($debug_==1) {
        echo"RCPT TO: <$email><BR>";
}
// get final output for validity testing
// (used)
$output = fgets ($fp, 2000);
                                  if($debug_==1) {
                                                echo"output : $output<BR>";
                                  }
// test the reply code from the mail
// server for the 550 (no recipient) code
if (ereg("^550",$output))
{
if($debug_==1) {
                                                 echo"Recipient doesnt exist<BR>";
                                          }
                                        // set our true/false(ness)
// array item for testing
$return[0] = false;
$return[1] = $output;

}
else
{
// otherwise, the address is valid,
// fillin the 2nd array item
// with the mail servers reply
// code for user to test if they
// want
$return[0] = true;
$return[1] = $output;
                                                if($debug==1) {
                                                 echo"The recipient exists <BR>";
                                                }
}

// tell the mail server we are done
// talking to it
fputs ( $fp, "QUIT\r\n");
                                  if($debug==1) {
                                                echo"Quit";
                                 }
// close the file pointer
fclose($fp);

// if we got a good value break,
// otherwise, we'll keep
// trying MX records until we get a good
// value, or we
// exhaust our possible MX servers
if ($return[0] == false) {
                                                if($debug_==1) {
                                                        echo"Recipient doesnt exist... Breaking";
                                                }        
                                        break;
                                  }
}
}
}
} else {
// No MX record appears for the specified Top-Level Domain; possibly
// an invalid host/domain name was specified.
$return[0] = false;
$return[1] = "Invalid email address (bad domain name)";
$return[2] = false;
} // end if checkdnsrr()

// return the array for the user to test against
return $return;
}
?>



email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
A web-based php3 IMAP email client supporting address books, attachements (downloading and sending), LDAP searching, and much much more.
Categories : Email, PHP, LDAP
Encoding data using PGP via PHP's proc_* functions
Categories : Cryptography, Security, Email, PHP, PGP
PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, Filesystem
email validator check checker email e-mail email address
Categories : PHP, Email, Regexps
Mail-lib provides a simple interface to the sendmail program. Note: you must actually have sendmail on your machine (sorry windows NT users).
Categories : Algorithms, Email, PHP
PHPRecommend v1.0 - "Recommend this page to a friend" script written in PHP. Easy to install
Categories : PHP, URLs, Complete Programs, Email, Site Planning
validateEmail 2.0 - upgraded version of the old validateEmail function used to validate email addresses via SMTP and regex.
Categories : Email, Regexps, PHP
Validator - A PHP class that can can be used for validating Email IDs and Dates
Categories : PHP, PHP Classes, Data Validation, Email, Date Time
making links from text
Categories : PHP, Regexps, Email
HTTP Basic Authentication via POP3.
Categories : Authentication, HTTP, Email, PHP
A PHP based webmail at : http://www.horde.org/imp
Categories : Email, IMAP, PHP, Complete Programs
Protect your mailto: email addresses from bots - pure PHP
Categories : PHP, Email, Security
Protect your email links from being spidered by spam email robots!
Categories : PHP, Security, Mail, Email
 Ket Majmudar wrote :453
The script proved very useful - I added a small section to return an invalid HTML response and a valid HTML response which would then use sendmail to notify the website owner that the email input was correct - I can post up the changes if any one is interested - only some minor alterations - i.e. all of the mail server responses have been removed