2013-10-07 86 views
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; 

如果我改变与突破继续,它的工作原理,但我想如果没有发送的电子邮件警告。如果我测试这个错误的消息是:失败:553对不起,该域不在我允许的rcpthosts列表中(#5.7.1)。

你能帮我吗?谢谢!

回答

0

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

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