0

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)。

你能帮助我吗?谢谢!

4

1 回答 1

0

当您的邮件服务器被限制为开放中继时会发生 553 错误(这是一件好事,或者您可以为互联网上的任何人转发任何电子邮件)。通常,当您使用 mail() 时,您使用的是本地服务器的 MTA(如果这是 Linux,那就是 sendmail)。因此,您的远程服务器不能用于以这种方式发送您的电子邮件。要么,要么您需要联系远程服务器并将运行此脚本的服务器列入白名单(首选)或允许您尝试发送到的域在中继中(这可能允许任何人中继到这些域)

出于好奇,你为什么不使用mail()?这比打开原始套接字要简单得多。

于 2013-10-07T12:38:01.360 回答