我一直在寻找解决方案,但是即使在查看了几个类似的线程之后,我也无法找到适合我情况的解决方案。
我通过一个测试脚本运行它并得到这个输出:
string(33) "Failed to Initiate Authentication"
在多次尝试解决问题后,我无法弄清楚导致错误的原因。
这是我的代码:
<?php
$SMTP_SERVER = 'relay-hosting.secureserver.net';
$SMTP_PORT = 25;
$SMTP_USERNAME = 'no-reply@website.com';
$SMTP_PASSWORD = 'password';
$SMTP_FROM = 'no-reply@website.com';
function smtpmail($to, $subject, $message, $headers = ''){
// set as global variable
global $SMTP_SERVER, $SMTP_PORT, $SMTP_USERNAME, $SMTP_PASSWORD, $SMTP_FROM;
// get From address
if ( $headers && preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/", $headers, $froms) ){
preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/", $froms[0], $fromarr);
$from = $fromarr[0];
}else{
$from = $SMTP_FROM;
$headers = 'From: '.$from."\r\n".$headers;
}
// Clean some of this stuff up...
// escape the message
$message = str_replace("\n.", "\n..", $message);
// also escape any leading period
if($message[0] == '.'){
$message = '.'. $message;
}
// escape the subject
$subject = str_replace( array("\r", "\n"), '', $subject );
// escape the recipient
$to = str_replace( array("\r", "\n"), '', $to );
// making sure not to send a zero, 'null', or etc. ambiguity ftw..</sarcasm>
if(!$headers) $headers = '';
// Open an SMTP connection
$cp = fsockopen ($SMTP_SERVER, $SMTP_PORT, &$errno, &$errstr, 1);
if (!$cp)
return "Failed to even make a connection";
$res=fgets($cp,256);
if(substr($res,0,3) != "220") return "Failed to connect";
// Say hello...
fputs($cp, "HELO ".$SMTP_SERVER."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Failed to Introduce";
// perform authentication
fputs($cp, "auth login\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "334") return "Failed to Initiate Authentication";
fputs($cp, base64_encode($SMTP_USERNAME)."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";
fputs($cp, base64_encode($SMTP_PASSWORD)."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "235") return "Failed to Authenticate";
// Mail from...
fputs($cp, "MAIL FROM: <$from>\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "MAIL FROM failed";
// Rcpt to...
fputs($cp, "RCPT TO: <$to>\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "RCPT TO failed";
// Data...
fputs($cp, "DATA\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "354") return "DATA failed";
// Send To:, Subject:, other headers, blank line, message, and finish
// with a period on its own line (for end of message)
fputs($cp, "To: $to\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Message Body Failed";
// ...And time to quit...
fputs($cp,"QUIT\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "221") return "QUIT failed";
return true;
}
//var_dump(smtpmail('mramonster@hotmail.com', 'email subject', ".\r\njust a: (message) 'that'\nwill <a href='http://www.google.com/'>hopefully</a> %get #through\r\n.\r\nsomething","From: test@example.com\r\n"));
?>
我已经仔细检查了用户名、服务器、密码、电子邮件等。我仍然无法弄清楚错误可能是什么。
注意:我不使用“no-reply@website.com”或“密码”作为我的凭据。