smtp电子邮件类输出导致pipe.php错误

时间:2011-12-21 18:34:53

标签: php email smtp pipe

我正在尝试将传入的电子邮件传输到php脚本

并从那里回复他们 即时通讯使用smtp邮件类,它工作正常 你可能知道,现在在管道 php脚本不应发送任何输出或失败

当我用简单的邮件功能回复传入的电子邮件时,一切正常但是只要我在页面中包含我的smtp类,我就会收到邮件传递失败以及我的回复!

如果我使用smtp类发送回复,我只会收到发送失败的电子邮件  现在我的猜测是我应该看到smtp类的输出

这是我的smtp类:

<?php
class Mail {
    private $smtpServer = 'you.smtp_server.com';
    private $port = '25';
    private $timeout = '45';
    private $username = 'address@you_domain.com';
    private $password = 'YouPassword';
    private $newline = "\r\n";
    private $localdomain = 'you_domain.com';
    private $charset = 'iso-8859-1';
    private $contentTransferEncoding = false;

    // Do not change anything below
    private $smtpConnect = false;
    private $to = false;
    private $subject = false;
    private $message = false;
    private $headers = false;
    private $logArray = array(); // Array response message for debug
    private $Error = '';

    public function __construct($to, $subject, $message , $smtp_id=1) {
        $smtp = setting::get_smtp($smtp_id);
        if($smtp){
                $this->from       = $smtp->from;
        $this->username   = $smtp->username;
        $this->password   = $smtp->password;
        $this->smtpServer = $smtp->address;
        $this->port       = $smtp->port;
        $this->localdomain= $smtp->from;
        $this->to = &$to;
        $this->subject = &$subject;
        $this->message = &$message;
        // Connect to server
        if(!$this->Connect2Server()) {
            // Display error message
            echo $this->Error.$this->newline.'<!-- '.$this->newline;
            print_r($this->logArray);
            echo $this->newline.'-->'.$this->newline;
            return false;
        }
        return true;
        }
    }

    private function Connect2Server() {
        // Connect to server
        $this->smtpConnect = fsockopen($this->smtpServer,$this->port,$errno,$error,$this->timeout);
        $this->logArray['CONNECT_RESPONSE'] = $this->readResponse();

        if (!is_resource($this->smtpConnect)) {
            return false;
        }
        $this->logArray['connection'] = "Connection accepted: $smtpResponse";
        // Hi, server!
        $this->sendCommand("EHLO $this->localdomain");
        $this->logArray['EHLO'] = $this->readResponse();
        // Let's know each other
        $this->sendCommand('AUTH LOGIN');
        $this->logArray['AUTH_REQUEST'] = $this->readResponse();
        // My name...
        $this->sendCommand(base64_encode($this->username));
        $this->logArray['REQUEST_USER'] = $this->readResponse();
        // My password..
        $this->sendCommand(base64_encode($this->password));
        $this->logArray['REQUEST_PASSWD'] = $this->readResponse();
        // If error in response auth...
        if (substr($this->logArray['REQUEST_PASSWD'],0,3)!='235') {
            $this->Error .= 'Authorization error! '.$this->logArray['REQUEST_PASSWD'].$this->newline;
            return false;
        }
        // "From" mail...
        $this->sendCommand("MAIL FROM: $this->from");
        $this->logArray['MAIL_FROM_RESPONSE'] = $this->readResponse();
        if (substr($this->logArray['MAIL_FROM_RESPONSE'],0,3)!='250') {
            $this->Error .= 'Mistake in sender\'s address! '.$this->logArray['MAIL_FROM_RESPONSE'].$this->newline;
            return false;
        }
        // "To" address
        $this->sendCommand("RCPT TO: $this->to");
        $this->logArray['RCPT_TO_RESPONCE'] = $this->readResponse();
        if (substr($this->logArray['RCPT_TO_RESPONCE'],0,3)!='250') {
            $this->Error .= 'Mistake in reciepent address! '.$this->logArray['RCPT_TO_RESPONCE'].$this->newline;
        }
        // Send data to server
        $this->sendCommand('DATA');
        $this->logArray['DATA_RESPONSE'] = $this->readResponse();
        // Send mail message
        if (!$this->sendMail()) return false;
        // Good bye server! =)
        $this->sendCommand('QUIT');
        $this->logArray['QUIT_RESPONSE'] = $this->readResponse();
        // Close smtp connect 
        fclose($this->smtpConnect);
        return true;
    }
    // Function send mail
    private function sendMail() {
        $this->sendHeaders();
        $this->sendCommand($this->message);
        $this->sendCommand('.');
        $this->logArray['SEND_DATA_RESPONSE'] = $this->readResponse();
        if(substr($this->logArray['SEND_DATA_RESPONSE'],0,3)!='250') {
            $this->Error .= 'Mistake in sending data! '.$this->logArray['SEND_DATA_RESPONSE'].$this->newline;
            return false;
        }
        return true;
    }
    // Function read response
    private function readResponse() {
        $data="";
        while($str = fgets($this->smtpConnect,4096))
        {
            $data .= $str;
            if(substr($str,3,1) == " ") { break; }
        }
        return $data;
    }
    // function send command to server
    private function sendCommand($string) {
        fputs($this->smtpConnect,$string.$this->newline);
        return ;
    }
    // function send headers
    private function sendHeaders() {
        $this->sendCommand("Date: ".date("D, j M Y G:i:s")." +0700");
        $this->sendCommand("From: <$this->from>");
        $this->sendCommand("Reply-To: <$this->from>");
        $this->sendCommand("To: <$this->to>");
        $this->sendCommand("Subject: $this->subject");
        $this->sendCommand("MIME-Version: 1.0");
        $this->sendCommand("Content-Type: text/html; charset=$this->charset");
        if ($this->contentTransferEncoding) $this->sendCommand("Content-Transfer-Encoding: $this->contentTransferEncoding");
        $this->sendCommand($this->newline);
        return ;
    }

    public function __destruct() {
        if (is_resource($this->smtpConnect)) fclose($this->smtpConnect);
    }
}
?>

1 个答案:

答案 0 :(得分:0)

Checkout phpmailer - 它具有各种功能,易于使用,文档齐全且支持社区。部署可能比调试自定义SMTP实现花费的时间更少。以下是示例项目的片段...

function sendEmail($to, $message, $message_txt)
{       
    require_once('phpmailer/class.phpmailer.php');

    $mail             = new PHPMailer();
    $body             = $message;
    $body             = @eregi_replace("[\]",'',$body);
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.gmail.com"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
    $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
    $mail->Username   = "email@gmail.com";  // GMAIL username
    $mail->Password   = "password";            // GMAIL password
    $mail->SetFrom('email@gmail.com', 'John');
    $mail->AddReplyTo("email@gmail.com", "John");
    $mail->Subject    = "Subject";

    $mail->AltBody    = $message_txt; // optional, comment out and test

    $mail->MsgHTML($body);

    $address = $to;
    $mail->AddAddress($address);

    return $mail->Send();
}

sendEmail('john@domain.com', '<b>hey john</b>', 'hey john');