windwos环境下发邮件没有问题,lunix下报错:auth password error: 421 out of memory (#4.3.0)
这是什么问题,代码问题,还是邮件服务器设置问题<?php
class Mailer {

public $smtpserver = 'localhost'; //smtp 服务器地址
public $smtp_prot = 25; //服务端口
public $time_out = 60; //允许连接等待时间
public $smtp_socket = ''; //返回的连接指针
public $plamt = 'windows'; //smtp 服务器所属操作系统

public $mail_from = ''; //发件人
public $mail_to = array(); //收件人
public $user = ''; //smtp 服务器用户名
public $pass = ''; //密码
public $auth = true; //是否需要验证 public $mail_type = 'html'; //发送内容的类型: text/html
public $subject = ''; //发送主题
public $body = ''; //发送内容

public $debug = false; //是否输出debug信息
public $error_message = ''; //发送过程相关的错误信息
public $response_message = ''; //服务器响应信息
public $recodes = array(); //每个命令的期望返回码

/**
 * Enter 构造方法,初始化一些基本参数
 *
 * @param unknown_type $smtpserver smtp 服务器地址
 * @param unknown_type $port 服务端口:25(默认)
 * @param unknown_type $auth 是否需要验证:true(默认)
 * @param unknown_type $debug 是否输出debug信息:FALSE(默认)
 * @param unknown_type $time_out 允许连接等待时间:60(默认)
 */
public function __construct($smtpserver, $port = 25, $auth = true, $debug = false, $time_out = 60){
$this->smtpserver = $smtpserver;
$this->smtp_prot = $port;
$this->auth = $auth;
$this->debug = $debug;
$this->time_out = $time_out;
$this->recodes = $this->getExpReCodes();
}

//获得邮件服务器的连接
public function openSocket(){

$conn = fsockopen($this->smtpserver, $this->smtp_prot, $errno, $errstr, $this->time_out);
if($conn){
$this->smtp_socket = $conn;
$this->send_message = 'Connect stmpserver: ['.$this->smtpserver.
']; port: ['.$this->smtp_prot.'] success.';
$this->log_message();

}else {
$this->send_message = $errstr.'('.$errno.')<br />\n';
$this->log_message();
}
return $conn;
}

/**
 * Enter 设置发送人的emai,用户名,密码
 *
 * @param unknown_type $from 发件人地址
 * @param unknown_type $user 发件人用户名
 * @param unknown_type $pass 密码
 */
public function setMailFrom($from, $user, $pass){
if($this->auth){
if($from && $user && $pass){
$this->mail_from = $from;
$this->user = $user;
$this->pass = $pass;
}else {
$this->send_message = 'from-email, user or pass is null';
$this->log_message();
}
}
}

/**
 * Enter 设置收件人地址
 *
 * @param unknown_type $to 收件人地址
 */
public function setMailTo($to){
if($to ){
if(is_array($to)){
$this->mail_to = explode(';', $to);
}else {
$this->mail_to[] = $to;
}

}else {
$this->send_message = 'to-email is null';
$this->log_message();
} //$this->mail_to = $to;

}

/**
 * Enter 设置邮件格式
 *
 * @param unknown_type $type 邮件格式:HTML(默认)
 */
public function setMailType($type = 'HTML'){
$this->mailtype = strtolower($type);
}

/**
 * Enter 设置邮件内容
 *
 * @param unknown_type $subject 主题
 * @param unknown_type $body 正文
 */
public function setContent($subject = 'No subject', $body){
$this->subject = $subject;
$this->body = $body;
}

/**
 * 设置行结束符:
 * - \r\n will work under dos and windows.
        * - \n will work for linux, unix and BSDs.
       * - \r will work for macs.
 *
 * @param unknown_type $line_break
 */
public function setPlamt($plamt = 'windows'){
$this->plamt = $plamt;
}

/**
 * Enter 基本邮件头信息
 *
 * @return unknown
 */
public function getBaseMailHeads(){
$heads = array('MIME-Version' => '1.0',
        'Content-Type' => 'text/'.($this->mail_type).'; charset=gb2312; format=flowed',
   'Content-Transfer-Encoding' => '8Bit' 
        //'X-Mailer' => 'Test By Tom'
     );
  
   return $heads;
}

/**
 * 发送邮件
 *
 * @return unknown 成功返回true,失败返回false
 */
public function sendMail(){
$line_break = "\r\n";
if($this->plamt == 'lunix'){
$line_break = "\n";
}elseif ($this->plamt == 'macs'){
$line_break = "\r";
}
$loc_host = 'localhost';//可任意
$heads = $this->getBaseMailHeads();//获得基本邮件头部信息
$message = array();
        
$fp = $this->openSocket();
if(!$fp){
$this->error_message = '<b>Error:</b> Cannot connect to '.$this->smtpserver.'<br>';
return false;
}
if(!$this->chekckCommOk('open')){// 期望返回220
//$this->error_message = '<b>Error:</b> Open connect to '.$this->smtpserver.'<br>';
return false;
}

if(!$this->excCommand("EHLO ".$loc_host.$line_break, 'ehlo')){
return false;
}
if($this->auth){//需要验证用户
if(!$this->excCommand("AUTH LOGIN ".$line_break, 'auth')){
return false;
}
if(!$this->excCommand(base64_encode($this->user).$line_break, 'user')){
return false;
}
if(!$this->excCommand(base64_encode($this->pass).$line_break, 'pass')){
return false;
}


}

//发件人地址
if(!$this->excCommand("MAIL FROM:<".$this->mail_from.">".$line_break, 'mailfrom')){
return false;
}

//收件人地址
if(!$this->excCommand("RCPT TO:<".$this->mail_to[0].">".$line_break, 'rcptto')){
return false;
}

//告诉服务器开始发送数据
if(!$this->excCommand("DATA".$line_break, 'data')){
return false;
}

//发件人地址
if(!$this->excCommand("FROM: ".$this->mail_from.$line_break)){
return false;;
}

//收件人地址
if(!$this->excCommand("TO: ".$this->mail_to[0].$line_break)){
return false;
}

//$this->excCommand("Subject: ".$this->subject.$line_break);
//主题
if(!$this->excCommand("Subject: ".$this->mime_header_encode($this->subject).$line_break)){
return false;
}

foreach ($heads as $key=>$value){//基本头部信息
$comm = $key.': '.$this->mime_header_encode($value).$line_break;
if(!$this->excCommand($comm)){
return false;
}
}

//发送一个空行,表示header部分发送完了
if(!$this->excCommand($line_break)){
return false;
}
//正文
if(!$this->excCommand(iconv( "UTF-8", "gb2312//IGNORE" , $this->body).$line_break)){
return false;
}
//$this->excCommand($this->body.$line_break);
//发送一个结束符号,表示数据发送完了
if(!$this->excCommand(".".$line_break, 'end')){
return false;
}

//退出邮件服务器
if(!$this->excCommand("QUIT".$line_break, 'quit')){
return false;
}
// WHILE($result = fgets($this->smtp_socket, 512)){
// $message[] = $result;
// }

            fclose($fp);//关闭网络连接
           
            return true;
           
}

public function excCommand($command, $key = ''){
if($command != ''){
fputs($this->smtp_socket, $command);
if(!empty($key)){
return $this->chekckCommOk($key);
}
return true;
}
return false;
}

public function chekckCommOk($key){
while($result = fgets($this->smtp_socket, 512)){
$this->response_message .= $result.'</br>';
if(substr($result,3,1) == " ") { 
if($this->recodes[$key][0] != substr($result,0,3)){
$this->error_message .= $this->recodes[$key[1].': '.$result.'</br>';
return false;
}
//echo $key.'->'.$result;
return true; 
}
}
return false;
}
//输出debug信息
public function log_message(){
if($this->debug){
echo $this->send_message;
}

}


// $test = new Mailer('mail.profit-china.com', 25, true, false, 60);
// $test->setMailFrom('[email protected]','[email protected]', 'tom');
// //$test->setMailTo('[email protected]');
//
//// $test = new Mailer('mail.163.com', 25, true, false, 60);
//// $test->setMailFrom('[email protected]','zfzhyjyh', 'xyjyh58924');
// //$test->setMailTo('[email protected]');
// $test->setMailTo('[email protected]');
// //$test->setMailTo('[email protected]');
//
// $test->setMailType('HTML');
// $test->setContent('test by tom '.' 测试', '<b><font color="red">test测试</font></b>');
// $test->sendMail();
//  print_r($test->response_message);
//  print_r($test->error_message);exit;
?>
邮件服务返回的所有信息:
220 mail.ccec.easia.cummins.com ESMTP
</br>250-mail.ccec.easia.cummins.com
</br>250-AUTH=LOGIN
</br>250-PIPELINING
</br>250 8BITMIME
</br>334 VXNlcm5hbWU6
</br>334 UGFzc3dvcmQ6
</br>421 out of memory (#4.3.0)
</br>错误信息:
auth password error: 421 out of memory (#4.3.0)</br>