Checking for live email addresses in PHP
Written by Mike James   
Monday, 23 August 2010
Article Index
Checking for live email addresses in PHP
SMTP
Final Function

Banner

SMTP commands

All SMTP commands and responses are text commands. You can even connect to an SMTP server using Telnet and conduct the session manually by typing in commands and reading the replies.

In this case we need to send:

HELO domain making the connection

The sever will respond with a greeting message and the SMTP transaction has started. Now we can send a batch of emails if we want to.

When SMTP first started you could deposit email into any SMTP server and expect it to be delivered to wherever it needed to go.That is they would relay mail from any user to any user.

Unfortunately spammers started to use arbitrary SMTP servers to send lots of emails to users who didn't want them. As a result most SMTP servers will not relay emails to other SMTP servers - and as a result most will only accept email to the domain that they serve, i.e. the one that the MX records correspond to.

The next command you need is:

MAIL FROM: from address

The from address is used as a return address and it might even be looked up to see if it is on a white/black list if the server is performing spam filtering.

To specify who the email is to you have to use:

RCPT TO: to address

After this you can send the body of the email as a multi-line text packet with the end marked by a line starting with a dot and nothing else.

However we aren't going to send any email body but use the:

RSET

command to abort the email we started to send.

To close the session you use the

QUIT 

command.

All of the commands are accepted by the server, processed and a response is sent back.

The response can be more than one line and this complicates things just a little. Each line, however, starts with a code that indicates the status of the process - 250 means everything is fine and the command has been acted upon.

We can check the status just before we RSET the email send and this will give us an indication that the server is at least ready to accept an email for the user. Notice that this isn't proof that the address exists on the server because some servers are set to swallow any mail sent to them to stop spammers from probing for valid email addresses - i.e. to stop exactly what we are attempting to do!

Now that we have the SMTP command worked out, we can continue the code.

First we check that there is a server to send commands to retrieve any sign on message:

if($sock){
$response= getResponse($sock);

We now have to write the function that reads the response. The simplest way is to read from the stream until we get a line feed carriage return pair:

function getResponse($sock){
$response="";
while(substr($response,-2)!=="\r\n"){
$data=fread($sock,4096);
if($data=="")break;
$response .=$data;
}
return $response;
}

This reads from the stream associated with the socket until there is no more data to read. It then returns the data read. 

We next send the HELO command and get the response:

fwrite($sock, "HELO mydomain\r\n");
$response= getResponse($sock);

Next we send the MAIL FROM command and once again get and ignore the response:

$response= getResponse($sock);
fwrite($sock,
"MAIL FROM: <me@mydomain.com>\r\n");
$response= getResponse($sock);

The next step is the important one as we ask the server to send the email to the user in question:

fwrite($sock,"RCPT TO: <".$email.">\r\n");
$response= getResponse($sock);
list($code,$msg) = explode(' ',$response);

This time we do process the response to see if the server is happy to send the email. All we do is split the line into the code that starts it and the rest of the message. If everything has worked the code should be 250. You can use a similar technique if you need to check the response code at other steps.

After sending the email addres we want to test we can reset the transaction and close the session and the socket:

fwrite($sock,"RSET\n");
$response= getResponse($sock);
fwrite($sock,"quit\n");
fclose($sock);

All that remains is to test the code we extracted earlier and exit the for loop if we have found a server that will accept mail for the user:

  if ($code == '250') {
$result= true;
break;
}
}
}

Notice that the break exits the nested if statements and the for loop.

All that remains is to put the error reporting level back to what it was and return the result:

 error_reporting($oldErrorLevel);
return $result;
}

Banner

<ASIN:1430227273>

<ASIN:0596805527>

<ASIN:1430232498>

<ASIN:0596157134>



Last Updated ( Monday, 23 August 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.