[英]PHP SMTP Script “Failed to Initiate Authentication” error
我一直在尋找一種解決方案,但是即使在審查了幾個類似的線程之后,我仍然找不到適合我情況的線程。
我通過測試腳本運行它並獲得以下輸出:
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”或“密碼”作為我的憑據。
為什么我們不能使用PEAR的Mail軟件包,它使生活變得輕松!
下載網址: http : //pear.php.net/package/Mail/redirected
PEAR的Mail包定義了一個接口,用於在PEAR層次結構下實現郵件程序。 它還提供了對多個郵件后端有用的支持功能。 當前支持的后端包括:PHP的本機mail()函數,sendmail和SMTP 。
這是一個代碼示例:
<?php
//Include the Pear Mail package
include("Mail.php");
/* mail setup recipients, subject etc */
$recipients = "feedback@yourdot.com";
$headers["From"] = "user@somewhere.com";
$headers["To"] = "feedback@yourdot.com";
$headers["Subject"] = "User feedback";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.mycorp.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtpusername";
$smtpinfo["password"] = "smtpPassword";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.