PHP SMTP 脚本 "Failed to Initiate Authentication"错误

标签 php html sendmail

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

我通过测试脚本运行它并获得以下输出:

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 包定义了一个接口(interface),用于在 PEAR 层次结构下实现邮件程序。它还提供对多个邮件程序后端有用的支持功能。目前支持的后端包括:PHP 的 native 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);

?>

关于PHP SMTP 脚本 "Failed to Initiate Authentication"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106473/

相关文章:

php - 不使用 iconv 删除重音

PHP Sendmail 队列优先级

php - SwiftMailer、PhpMailer 等 : Difference between mail() and sendmail

php - 我们如何将路径添加到数组中的子项?

php - EXCEL:如何在excel中添加背景标题颜色;使用 PHP 导出?

php - Symfony 表单不保存具有 ManyToMany 关系的实体

html - 当用户键入一些文本时,IE 文本区域宽度会发生变化

jquery - 如何将CSS箭头插入html页面并使其可点击

jquery - 使用JQuery在youtube <object>上添加视频覆盖

php - 联系表未发送