-2

I need to send order confirmations to my customers. Sales people asks for customers email address. But if sales person makes a typo writing down email address, how can I check for it ?

I have a piece of code from php.net. It checks recipients domain and email format but not if email exists.

Let's assume customer's email is like this : someone@domain.com

Code outputs like this :

email someone@domain.com, 0= not ok, 1= ok : 1 <-- correct

email someone@domain.co, 0= not ok, 1= ok : 0 <-- domain typo

email somepne@domain.com, 0= not ok, 1= ok : 1 <-- mailbox typo

It outputs 0 if domain has a typo, but 1 if mailbox part has a typo.

How can I add check for mailbox part in it ?

Code :

<?
/*
This script validates an e-mail adress using getmxrr and fsockopen

1. it validates the syntax of the address.
2. get MX records by hostname
3. connect mail server and verify mailbox(using smtp command RCTP TO:<email>)
When the function "validate_email([email])" fails connecting the mail server with the highest priority in the MX record it will continue with the second mail server and so on..
The function "validate_email([email])" returns 0 when it failes one the 3 steps above, it will return 1 otherwise
*/

$email = "someone@domain.com";
echo "email $email, 0= not ok, 1= ok : ".validate_email($email)."<br>";
$email = "someone@domain.co";
echo "email $email, 0= not ok, 1= ok : ".validate_email($email)."<br>";
$email = "somepne@domain.com";
echo "email $email, 0= not ok, 1= ok : ".validate_email($email)."<br>";

function validate_email($email){
   $mailparts=explode("@",$email);
   $hostname = $mailparts[1];

   // validate email address syntax
   $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$^";
   $b_valid_syntax=preg_match($exp, $email);

   // get mx addresses by getmxrr
   $b_mx_avail=getmxrr( $hostname, $mx_records, $mx_weight );
   $b_server_found=0;

   if($b_valid_syntax && $b_mx_avail){
     // copy mx records and weight into array $mxs
     $mxs=array();

     for($i=0;$i<count($mx_records);$i++){
       $mxs[$mx_weight[$i]]=$mx_records[$i];
     }

     // sort array mxs to get servers with highest prio
     ksort ($mxs, SORT_NUMERIC );
     reset ($mxs);

     while (list ($mx_weight, $mx_host) = each ($mxs) ) {
       if($b_server_found == 0){

         //try connection on port 25
         $fp = @fsockopen($mx_host,25, $errno, $errstr, 2);
         if($fp){
           $ms_resp="";
           // say HELO to mailserver
           $ms_resp.=send_command($fp, "HELO microsoft.com");

           // initialize sending mail
           $ms_resp.=send_command($fp, "MAIL FROM:<support@microsoft.com>");

           // try receipent address, will return 250 when ok..
           $rcpt_text=send_command($fp, "RCPT TO:<".$email.">");
           $ms_resp.=$rcpt_text;

           if(substr( $rcpt_text, 0, 3) == "250")
             $b_server_found=1;

           // quit mail server connection
           $ms_resp.=send_command($fp, "QUIT");

         fclose($fp);

         }

       }
    }
  }
  return $b_server_found;
}

function send_command($fp, $out){

  fwrite($fp, $out . "\r\n");
  return get_data($fp);
}

function get_data($fp){
  $s="";
  stream_set_timeout($fp, 2);

  for($i=0;$i<2;$i++)
    $s.=fgets($fp, 1024);

  return $s;
}

// support windows platforms
if (!function_exists ('getmxrr') ) {
  function getmxrr($hostname, &$mxhosts, &$mxweight) {
    if (!is_array ($mxhosts) ) {
      $mxhosts = array ();
    }

    if (!empty ($hostname) ) {
      $output = "";
      @exec ("nslookup.exe -type=MX $hostname.", $output);
      $imx=-1;

      foreach ($output as $line) {
        $imx++;
        $parts = "";
        if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
          $mxweight[$imx] = $parts[1];
          $mxhosts[$imx] = $parts[2];
        }
      }
      return ($imx!=-1);
    }
    return false;
  }
}

?>
Sparky
  • 57
  • 9
  • 1
    You can't check for it, unless you have all email address of all users of all mail server in the whole Internet! – Salim Ibrohimi May 11 '18 at 05:21
  • 2
    How do you know, that `somepne@domain.com` isn't a vaild email address? If you want to validate a users email adress, then send an email to the address and let the user confirm the receiving. – schlonzo May 11 '18 at 05:25
  • I can't know that it isn't. I was just assuming that it is a typo. – Sparky May 11 '18 at 05:30
  • Other reference on this subject https://www.webdigi.co.uk/blog/2009/how-to-check-if-an-email-address-exists-without-sending-an-email/ – Andreas May 11 '18 at 05:39

1 Answers1

-1

The most you can do is

a) Use regex to check if the entered string appears to be a valid email address see here

b) Ask to input the email twice, disallow copy/paste, compare and verify

I don't think it makes sense to run a spellchecker on email addresses.

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26
  • Maybe then I have to ask sales people to enter email address twice and compare it in my sales script. Disallowing copy/paste is a good idea too. This script already validates the syntax of the address. – Sparky May 11 '18 at 05:36
  • I don't think typing it twice will help at all. Remember the person hears the email on phone I assume(?) And then types in the computer. An easy example is "mopy" if you hear someone say that you assume it is "moby", typing it twice won't make any difference. – Andreas May 11 '18 at 05:49
  • the gold standard is to send an email to the address to check, not this. –  May 11 '18 at 06:08
  • +Andreas you have a pretty good point for not to ask them to enter it twice because sales persons indeed hears emails on phone. – Sparky May 11 '18 at 13:21