gpt4 book ai didi

php - 使用 PHP EHLO 进行 SMTP 验证

转载 作者:行者123 更新时间:2023-12-04 02:41:03 24 4
gpt4 key购买 nike

我正在尝试进行电子邮件验证,但似乎无法进行。目前它会与域建立连接,但之后似乎超时。有人知道为什么吗?好像卡在这里:

$res=fgets($connection, "EHLO $mydomain\n");

谁能告诉我这是为什么?我的代码如下:

if(isset($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))

{

// Could get this from the php ini?
$from="xxxx@xxxx-int.net";
$to=$_POST['email'];
$subject="Test";
$message="Testing";
list($me,$mydomain) = split("@",$from);

// Now look up the mail exchangers for the recipient
list($user,$domain) = split("@",$to,2);
if(getmxrr($domain,$mx,$weight) == 0) return FALSE;

// Try them in order of lowest weight first
array_multisort($mx,$weight);
$success=0;

foreach($mx as $host) {
// Open an SMTP connection
$connection = fsockopen ($host, 25, $errno, $errstr, 1);
if (!$connection)
continue;
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "220") echo $res;

// Introduce ourselves
fputs($connection, "EHLO $mydomain\n");
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "250") echo $res;

// Envelope from
fputs($connection, "MAIL FROM: $from\n");
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "250") echo $res;

// Envelope to
fputs($connection, "RCPT TO: $to\n");
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "250") echo $res;

// The message
fputs($connection, "DATA\n");
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "354") echo $res;

// Send To:, From:, Subject:, other headers, blank line, message, and finish
// with a period on its own line.
fputs($connection, "To: $to\nFrom: $from\nSubject: $subject\n$message\n.\n");
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "250") echo $res;

// Say bye bye
fputs($connection,"QUIT\n");
$res=fgets($connection);
echo $res;
if(substr($res,0,3) != "221") echo $res;

// It worked! So break out of the loop which tries all the mail exchangers.
$success=1;
break;
}
// Debug for if we fall over - uncomment as desired
// print $success?"Mail sent":"Failure: $res\n";
if($connection) {
if($success==0) fputs($connection, "QUIT\n");
fclose ($connection);
}
return $success?TRUE:FALSE;
}

最佳答案

  fputs($connection, "EHLO $mydomain\n"); 

你只在最后发送换行,但是smtp rfc需要 CRLF。这会导致您的脚本“挂起”,因为电子邮件服务器不会响应您的无效行结尾。你必须将其更改为

  fputs($connection, "EHLO $mydomain\r\n");

等等

请注意,在许多情况下,电子邮件验证将不起作用,即使您让脚本与 RFC 兼容的 SMTP 对话。许多服务器简单地接受(然后退回)所有发给无效收件人的邮件。其他人在数据阶段进行收件人验证,而不是 rcpt to (exchange 2013..sigh) 等。

关于php - 使用 PHP EHLO 进行 SMTP 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19931812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com