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;
如果我用 continue 更改 break,它可以工作,但我想在未发送电子邮件时发出警告。如果我测试此错误,则消息是:失败:553 抱歉,该域不在我允许的 rcpthosts 列表中(#5.7.1)。
你能帮助我吗?谢谢!