Linux服务器上的PHP - 如何验证该电子邮件地址是否真实

时间:2013-03-22 23:49:20

标签: php linux shell telnet

我在Linux服务器上有一个PHP脚本,需要确定电子邮件地址是真实的还是假的。

我正在尝试使用telnet,从期望脚本启动,但它不起作用。

PHP脚本的用法是:

$retVal = shell_exec( "./atelnet.sh mailserver.net fake@example.com" ) ;

任何帮助都会受到赞赏,或者更好的方法。

谢谢。

这是我到目前为止所拥有的:

#!/usr/bin/expect
#
# name: atelnet.sh
# usage from php: 
# $retVal = shell_exec( "./atelnet.sh mailserver.net fake@example.com" ) ;
#
# if goes wrong, the script will timeout in 20 seconds
set timeout 20

# first argument assigned to the variable "server"
set server [lindex $argv 0]

# second argument assigned to the variable "emailaddress"
set emailaddress [lindex $argv 1]

# spawn telnet and connect to variable "server" on port 25
spawn telnet $server 25

# script expects "220 ..." telnet from server, telnet login ok response
# actual response looks like: 
# "220 mx.server.com ESMTP ni10si2925797obc.73 - gsmtp"
# or
# "220-host.server.com ESMTP Fri, 22 Mar 2013 01:48:03 -0000"
expect "220"

# script sends initial handshake greeting
send "HELO"

# script expects "250 ..." telnet from server, at-your-service ok response
# actual response looks like: 
# "250 mx.server.com at your service"
# or
# "250 host.server.com Hello host.server.com [111.222.333.444]"
expect "250"

# script sends from: email address (any address is fine)
send "mail from:<a@b.com>"

# script expects "250 ..." telnet from server, ok acknowledgement of from:email
# actual response looks like: 
# "250 2.1.0 OK v16si1294229qct.125 - gsmtp"
# or
# "250 OK"
expect "250"

# script sends query to the email address being tested
# PHP string cat syntax doesn't work !!!
# HOW ???
send "rcpt to:<" . $emailaddress . ">"

# script expects one of these:
# fake: "550-5.1.1 The email account that you tried to reach does not exist."
# or
# true: "250 Accepted"

# log server's response to file. HOW ???

编辑:

我正在添加我的PHP套接字代码,这似乎不起作用:

$smtp_server = fsockopen( $mx_hostname, 25, $errno, $errstr, 30 ) ;
$ret1 = fwrite( $smtp_server, "helo hi\r\n" ) ;
$ret2 = fwrite( $smtp_server, "mail from: <a@b.org>\r\n" ) ;
$ret3 = fwrite( $smtp_server, "rcpt to: <" . $unk_email_address. ">\r\n" ) ;

此代码的任何想法也会受到欢迎。

谢谢。

3 个答案:

答案 0 :(得分:1)

我可以看到预期的用法,但我会说实际连接并启动与收件人邮件服务器的握手有点过分。特别是因为仍然无法保证用户存在。

您应该在此阶段之前检查MX记录,请参阅getmxrr
假设您已经检查过将其验证为有效的电子邮件字符串,我会说将其与其他MX查找相结合就足够了。

如果您仍想这样做,我会使用PHP套接字而不是telnet来实现它。甚至可能使用expect extension,因为你已经开始了这条路。

其他想法可能是让Facebook和Google oauth在您的网站上登录,如果它与更高质量的用户注册相关

答案 1 :(得分:0)

试图发明轮子毫无意义。

检查例如:Zend_Validate_EmailAddress

您甚至可以验证主机名和mx记录

$validator = new Zend_Validate_EmailAddress(
    array(
        'allow' => Zend_Validate_Hostname::ALLOW_DNS,
        'mx'    => true
    )
);

通过验证现有的域名和电子邮件地址格式,我认为应该足够了。

答案 2 :(得分:0)

您可以使用PHP过滤器验证电子邮件:

http://php.net/manual/en/filter.filters.validate.php

$email_a = 'user@example.com';


if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (email_a) email address is considered valid.";
}