2

我一直在寻找解决方案,但是即使在查看了几个类似的线程之后,我也无法找到适合我情况的解决方案。

我通过一个测试脚本运行它并得到这个输出:

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”或“密码”作为我的凭据。

4

1 回答 1

0

为什么我们不能使用让生活变得轻松的 PEAR 邮件包!

下载地址: 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);

?>
于 2013-01-01T08:00:43.933 回答