使用php查找有效的电子邮件地址


Finding Valid Email address using php

我想开发一个应用程序来获取电子邮件地址,我需要向该电子邮件id发送确认,在此之前我需要检查输入的电子邮件id是否有效。是否有可能检查使用php或jsp?

如果validate指的是检查字符串是否可以是电子邮件地址可以使用以下PHP行

$email = filter_var($email, FILTER_VALIDATE_EMAIL);

之后,如果变量不为空,则电子邮件地址有效(检查http://php.net/filter_var)

你不明白这件事。

发送确认邮件的目的是检查该邮件是否存在,是否有效,是否被用户阅读。

不要重新发明轮子,不要听当地的"理论家",他们从未使用过他们在这里兜售的工具。
发送确认电子邮件是判断是否存在这样一个帐户的唯一可靠方法。

function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/''.''./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9''-''.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/''.''./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(''''.|[A-Za-z0-9!#%&`_=''/$''*+?^{}|~.-])+$/',
                 str_replace("''''","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(''''"|[^"])+"$/',
             str_replace("''''","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
 ↪checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

Thanks to http://www.linuxjournal.com/article/9585

没有理由重新发明轮子,Zend框架提供了一个电子邮件验证库,查看文档了解更多信息。

看起来你可以这样做:

// Under EmailAddress section of docs
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($email)) {
    // email appears to be valid
} else {
    // email is invalid; print the reasons
    foreach ($validator->getMessages() as $message) {
        echo "$message'n";
    }
}

使用PHP检查SMTP是否存在邮件

http://www.webdigi.co.uk/blog/wp-content/uploads/2009/01/smtpvalidateclassphp.txt

例子
<?php
// the email to validate  
$email = 'joe@gmail.com';  
// an optional sender  
$sender = 'user@example.com';  
// instantiate the class  
$SMTP_Valid = new SMTP_validateEmail();  
// do the validation  
$result = $SMTP_Valid->validate($email, $sender);  
// view results  
var_dump($result);  
echo $email.' is '.($result ? 'valid' : 'invalid')."'n";  
// send email?   
if ($result) {  
  //mail(...);  
}
?>