使用SMTP和PHP验证电子邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用SMTP和PHP验证电子邮件相关的知识,希望对你有一定的参考价值。
This function validate a mail address in a smart way: if the address to validate has a correct syntax (checked with regular expression), it tries to connect to the mx server of the address. This means that the program have to use the smtp protocol to talk with each mx server registered in the dns for that domain address.When a connection with an mx server is established, the program presents himself with the $probe_address (if no probe_address is specified it will use the SERVER_ADMIN setting of the php.ini file) and asks to send a mail message to the address to validate. If the server responds that the destination mailbox is ok, it stops and return ok value, else it gives you the bad answer of the mail server.
function validateEmailSmtp($email, $probe_address="", $debug=false) { # -------------------------------- # function to validate email address # through a smtp connection with the # mail server. # by Giulio Pons # http://www.barattalo.it # -------------------------------- $output = ""; # -------------------------------- # Check syntax with regular expression # -------------------------------- if (!$probe_address) $probe_address = $_SERVER["SERVER_ADMIN"]; if (preg_match('/^([a-zA-Z0-9._+-]+)@(([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,7}|[0-9]{1,3})(]?))$/', $email, $matches)) { $user = $matches[1]; $domain = $matches[2]; # -------------------------------- # Check availability of DNS MX records # -------------------------------- # -------------------------------- # Construct array of available mailservers # -------------------------------- $mxs[$mxhosts[$i]] = $mxweight[$i]; } } else { } # -------------------------------- # Query each mailserver # -------------------------------- if($total > 0) { # -------------------------------- # Check if mailers accept mail # -------------------------------- for($n=0; $n < $total; $n++) { # -------------------------------- # Check if socket can be opened # -------------------------------- if($debug) { $output .= "Checking server $mailers[$n]... ";} $connect_timeout = 2; $errno = 0; $errstr = 0; # -------------------------------- # controllo probe address # -------------------------------- if (preg_match('/^([a-zA-Z0-9._+-]+)@(([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,7}|[0-9]{1,3})(]?))$/', $probe_address,$fakematches)) { # -------------------------------- # Try to open up socket # -------------------------------- if($debug) {$output .= "Opening up socket to $mailers[$n]... Success! ";} if($debug) { $output .= "$mailers[$n] replied: $response ";} # -------------------------------- # Be sure to set this correctly! # -------------------------------- "HELO $probe_domain", "MAIL FROM: <$probe_address>", "RCPT TO: <$email>", "QUIT", ); # -------------------------------- # Hard error on connect -> break out # -------------------------------- if ($codice=="421") { //421 #4.4.5 Too many connections to this host. $error = $response; break; } else { if($response=="" || $codice=="") { //c'è stato un errore ma la situazione è poco chiara $codice = "0"; } $error = "Error: $mailers[$n] said: $response "; break; } break; } foreach($cmds as $cmd) { "); if ($codice<>"552") { $error = "Unverified address: $mailers[$n] said: $response"; break 2; } else { $error = $response; break 2; } # -------------------------------- // il 554 e il 552 sono quota // 554 Recipient address rejected: mailbox overquota // 552 RCPT TO: Mailbox disk quota exceeded # -------------------------------- } } if($debug) { $output .= "Succesful communication with $mailers[$n], no hard errors, assuming OK ";} break; } elseif($n == $total-1) { $error = "None of the mailservers listed for $domain could be contacted"; $codice = "0"; } } else { $error = "Il probe_address non è una mail valida."; } } } elseif($total <= 0) { $error = "No usable DNS records found for domain '$domain'"; } } } else { $error = 'Address syntax not correct'; } if($debug) { } }
以上是关于使用SMTP和PHP验证电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:2.0.0 准备启动 TLS)]
php WP Mail SMTP:使用SMTP邮件程序时 - 在PHP 5.6+上禁用SSL验证
无法在“localhost”端口 587 连接到邮件服务器,验证 php.ini 中的“SMTP”和“smtp_port”设置或在 wamp 服务器中使用 ini_set()