3

我在 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" ) ;

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

谢谢你。

4

3 回答 3

1

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

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

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

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

于 2013-03-22T23:59:24.220 回答
0

试图发明轮子是没有意义的。

检查例如:Zend_Validate_EmailAddress

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

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

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

于 2013-03-22T23:55:50.163 回答
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.";
}
于 2013-03-23T00:00:25.180 回答