使用Auth的SMTP不起作用

时间:2014-11-13 15:32:47

标签: php smtp

我正在尝试使用此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";   

我得到了这个:

array(10) { 
    ["connection"]=> string(84) "Connected: 220-vm01.masterd.pt ESMTP Exim 4.84 #2 Thu, 13 Nov 2014 10:29:17 -0500 " 
    ["authrequest"]=> string(75) "220-We do not authorize the use of this system to transport unsolicited, " 
    ["authusername"]=> string(25) "220 and/or bulk e-mail. " 
    ["authpassword"]=> string(43) "503 AUTH command used when not advertised " 
    ["heloresponse"]=> string(26) "500 unrecognized command " 
    ["mailfromresponse"]=> string(26) "500 unrecognized command " 
    ["mailtoresponse"]=> string(43) "250-vm01.masterd.pt Hello [109.71.45.39] " 
    ["data1response"]=> string(19) "250-SIZE 52428800 " 
    ["data2response"]=> string(14) "250-8BITMIME " 
    ["quitresponse"]=> string(16) "250-PIPELINING " 
}

但电子邮件永远不会进入我的收件箱!

1 个答案:

答案 0 :(得分:0)

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

  1. &#34; EHLO $ localhost&#34;命令应在&#34; AUTH LOGIN&#34;

  2. 之前发送
  3. 用户名应与&#34; AUTH LOGIN&#34;在同一行发送,即:

    fputs($ smtpConnect,&#34; AUTH LOGIN&#34; .base64_encode($ username)。$ newLine);
    $ smtpResponse = fgets($ smtpConnect,515);
    $ logArray [&#39; authrequest&#39;] =&#34; $ smtpResponse&#34;;

  4. 当我每次使用echo函数打印出$ smptResponse时,对它进行故障排除有很大帮助。对于某些响应,您需要多次尝试响应,因为响应有多行。