简体   繁体   English

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

[英]PHP on Linux server - how to verify that email address is real

I have a PHP script on a Linux server that needs to determine whether an email address is real or fake. 我在Linux服务器上有一个PHP脚本,需要确定电子邮件地址是真实的还是假的。

I'm trying to use telnet for this, launched from an expect script, but it's not working. 我正在尝试使用telnet,从期望脚本启动,但它不起作用。

The PHP script usage would be: PHP脚本的用法是:

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

Any help would be appreciated, or a better approach altogether. 任何帮助将不胜感激,或完全更好的方法。

Thank you. 谢谢。

Here's what I have so far: 这是我到目前为止所拥有的:

#!/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 ???

EDIT: 编辑:

I'm adding below my PHP socket code, which doesn't seem to work: 我正在下面添加我的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" ) ;

Any ideas on this code would be welcomed as well. 关于此代码的任何想法也会受到欢迎。

Thank you. 谢谢。

I can see the intended usage but I would say that it is a little bit of an overkill to actually connect and initiate a handshake with the recipient mail server. 我可以看到预期的用途,但我会说实际连接并启动与收件人邮件服务器的握手有点过分。 Especially since there are still no guarantees that it means the user exists. 特别是因为仍然无法保证它意味着用户存在。

You should check for an MX record before this stage, see getmxrr 您应该在此阶段之前检查MX记录,请参阅getmxrr
Assuming you already checked validated it as a valid e-mail string I would say combining that with the additional MX lookup is enough. 假设您已经检查过将其验证为有效的电子邮件字符串,我会说将其与其他MX查找相结合就足够了。

If you still want to do it I would implement it using PHP sockets instead of telnet. 如果您仍想这样做,我会使用PHP套接字而不是telnet来实现它。 Maybe even using the expect extension since you already started that path. 也许甚至使用了expect扩展,因为你已经开始了这条路径。

Other ideas could be to have Facebook and Google oauth login on your website if it is related to higher quality user sign up 其他想法可能是让Facebook和Google oauth在您的网站上登录,如果它与更高质量的用户注册有关

There is no point trying to invent wheel. 尝试发明轮子毫无意义。

Check for example: Zend_Validate_EmailAddress 检查例如: Zend_Validate_EmailAddress

You can even validate hostname and mx record 您甚至可以验证主机名和mx记录

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

and by validating existing domain and email address format I would say it should be enough. 通过验证现有的域名和电子邮件地址格式我会说它应该足够了。

You can use PHP Filters to validate an E-mail: 您可以使用PHP过滤器来验证电子邮件:

http://php.net/manual/en/filter.filters.validate.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.";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM