function socketmail($to,$subject,$message,$headers,$debug=0)
{global $MAILFROM;
$from=$MAILFROM;
list($me,$mydomain) = split("@",$from);
// Now look up the mail exchangers for the recipient
list($user,$domain) = split("@",$from,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
//echo "$host To: $to<HR>";
//print "SMTP: $host\n";
$connection = fsockopen ($host, 25, &$errno, &$errstr, 1);
if (!$connection)
continue;
$res=fgets($connection,256);
if(substr($res,0,3) != "220") break;
// Introduce ourselves
fputs($connection, "HELO $mydomain\n");
$res=fgets($connection,256);
if(substr($res,0,3) != "250") break;
// Envelope from
fputs($connection, "MAIL FROM: $from\n");
$res=fgets($connection,256);
if(substr($res,0,3) != "250") break;
// Envelope to
fputs($connection, "RCPT TO: $to\n");
$res=fgets($connection,256);
//print "Response: $res\n";
if(substr($res,0,3) != "250") break;
// The message
fputs($connection, "DATA\n");
$res=fgets($connection,256);
if(substr($res,0,3) != "354") break;
// Send To:, From:, Subject:, other headers, blank line, message, and finish
// with a period on its own line.
fputs($connection, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$res=fgets($connection,256);
if(substr($res,0,3) != "250") break;
// Say bye bye
fputs($connection,"QUIT\n");
$res=fgets($connection,256);
if(substr($res,0,3) != "221") break;
// 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
if($debug)
{
print $success?"Mail sent":"Failure: $res\n";
die(0);
}
if($connection)
{
if($success==0)
fputs($connection, "QUIT\n");
fclose ($connection);
}
return $success?TRUE:FALSE;
有代码。问题出在这里:
fputs($connection, "RCPT TO: $to\n");
$res=fgets($connection,256);
//print "Response: $res\n";
if(substr($res,0,3) != "250") break;
如果我用继续更改中断,它可以工作,但我想警告是否未发送电子邮件。如果我测试此错误消息是:失败:553抱歉,该域名不在我允许的rcpthosts列表中(#5.7.1)。
你能帮帮我吗?谢谢!答案 0 :(得分:0)
当您的邮件服务器被限制为开放中继时,会发生553错误(这是一件好事,或者您可以为互联网上的任何人转发任何电子邮件)。通常,当您使用mail()时,您正在使用本地服务器的MTA(如果这是Linux,那将是sendmail)。因此,您的远程服务器不能用于以这种方式发送您的电子邮件。要么是您需要联系远程服务器并将此脚本运行的服务器列入白名单(首选),要么允许您尝试在中继中发送的域(这可能允许任何人转发到这些域)
出于好奇,你为什么不使用mail()?它比打开原始套接字要简单得多。