SMPT protocol check of email address at preferred MX host or host.
This returns nothing if email is valid, or a nice descriptive error message. It will split out the domain, lookup the preferred mailhost, connect to it an try to deliver at the given address. If no MX record is found it will try to deliver directly at the SMTP port. Since most mailservers say that the given address has valid syntax the only more waterproof check would be to send mail and wait for a reply.
This is the fullest implementation
By : datasmid
<?php
function checkemail($email){
list($mailbox,$domain) = split('@',$email,2);
$state = 'domain';
// find preferred mailserver
if(getmxrr($domain,$mailhosts,$pref)){
asort($pref);
foreach($pref as $preferred){
$mailserver = $mailhosts[key($pref)];
break;
}
$state = "trying mailserver $mailserver";
$state = mailconnect($mailserver,$email);
}else{
// no mail exchange found try as host
$state = "No MX, trying $domain";
$state = mailconnect($domain,$email);
}
return $state;
}
function mailconnect($mailserver,$email){
$myhostname = $SERVER_NAME;
$connection = fsockopen($mailserver, 25);
if($connection){
$state = "connected to $mailserver";
// Nothing to do with greeting
//$smtpgreeting = fread($connection, 512);
//if($smtpgreeting){
fputs($connection, "HELO $myhostname\r\n");
$hello = fgets($connection, 512);
if($hello){
$state = "chatting to $mailserver: $hello";
fputs($connection, "MAIL FROM: <webserver@$myhostname>\r\n");
$youok = fgets($connection, 512);
if($youok){
$state = "chatting to $mailserver: $youok";
fputs($connection, "RCPT TO: <$newaddress>\r\n");
$recepient = fgets($connection, 512);
$state = "chatting to $mailserver: $recepient";
if(ereg('250',$recepient)){
fputs($connection, "QUIT\r\n");
$deliverable = true;
$state = false;
}elseif(ereg('220',$recepient)){
fputs($connection, "QUIT\r\n");
$deliverable = true;
$state = false;
}else{
$deliverable = false;
$state = "RCPT? $recepient $newaddress";
}
}
}else{
$state = "$mailserver not accepting mail now, please try again.";
}
//}else{
//$state = 'mailserver not greeting me';
//break;
//}
}else{
$state = "$mailserver not listening";
}
return $state;
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Email Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |