带有身份验证的 SMTP 不起作用


SMTP with Auth doesnt work

我正在尝试使用此PHP脚本发送电子邮件,但由于某种原因,我没有收到电子邮件。

$to = my email;  
$nameto = "Who To";
$from = my second email;
$namefrom = "Who From";  
$subject = "Hello World Again!";
$message = "World, Hello!";  
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);  
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message) {  
$smtpServer = my smtp server  
$port = "25";  
$timeout = "30";  
$username = my second email;
$password = my password 
$localhost = my smtp server
$newLine = "'r'n";  
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);  
$smtpResponse = fgets($smtpConnect, 515);  
if(empty($smtpConnect))   
{  
  $output = "Failed to connect: $smtpResponse";  
  return $output;  
}  
else 
{  
  $logArray['connection'] = "Connected: $smtpResponse";  
}  
fputs($smtpConnect,"AUTH LOGIN" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authrequest'] = "$smtpResponse";  
fputs($smtpConnect, base64_encode($username) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authusername'] = "$smtpResponse";  
fputs($smtpConnect, base64_encode($password) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authpassword'] = "$smtpResponse";  
//fputs($smtpConnect, "EHLO $localhost" . $newLine);  
fputs($smtpConnect, "EHLO" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['heloresponse'] = "$smtpResponse";  
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailfromresponse'] = "$smtpResponse";  
fputs($smtpConnect, "RCPT TO: $to" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailtoresponse'] = "$smtpResponse";  
fputs($smtpConnect, "DATA" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data1response'] = "$smtpResponse";  
$headers = "MIME-Version: 1.0" . $newLine;  
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
$headers .= "To: $nameto <$to>" . $newLine;  
$headers .= "From: $namefrom <$from>" . $newLine;  
fputs($smtpConnect, "To: $to'nFrom: $from'nSubject: $subject'n$headers'n'n$message'n.'n");  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data2response'] = "$smtpResponse";  
fputs($smtpConnect,"QUIT" . $newLine);   
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['quitresponse'] = "$smtpResponse";   

我得到这个:

数组(10) {    ["连接"]=> 字符串(84) "已连接: 220-vm01.masterd.pt ESMTP Exim 4.84 #2 星期四, 13 十一月 2014 10:29:17 -0500 "    ["authrequest"]=> string(75) "220-我们不授权使用此系统进行未经请求的传输,"    ["authusername"]=> string(25) "220 和/或批量电子邮件。    ["authpassword"]=> string(43) "未通告时使用 503 AUTH 命令"    ["heloresponse"]=> string(26) "500 无法识别的命令"    ["mailfromresponse"]=> string(26) "500 无法识别的命令"    ["mailtoresponse"]=> string(43) "250-vm01.masterd.pt Hello [109.71.45.39] "    ["data1response"]=> string(19) "250-SIZE 52428800"    ["data2response"]=> string(14) "250-8BITMIME "    ["退出响应"]=> 字符串(16) "250-流水线"}

但是电子邮件从未发送到我的收件箱!

我遇到了同样的问题,直到我进行了以下更改:

  1. "EHLO $localhost"命令应在"身份验证登录"之前发送

  2. 用户名应与"AUTH LOGIN"在同一行发送,即:

    fputs($smtpConnect,"AUTH LOGIN" . base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authrequest'] = "$smtpResponse";

当我每次使用回声功能打印出$smptResponse时,对故障排除有很大帮助。 对于某些响应,您需要多次对响应进行 fgets ,因为响应具有多行。