我的页面显示 :unable to authenticate to smtp server
不知道什么原因,上网也看了别人的发的这个错误.,但很少,别人也贴了解决办法,但我没看到适合我的.所以来求高手帮解决!在我的SMTP。PHP下有这个,我邮箱的用户密码都对的哦。但我不知道什么原因哦。艾。。求高手指点。        if ($this->auth) {
            $method = is_string($this->auth) ? $this->auth : '';            if (PEAR::isError($smtp->auth($this->username, $this->password,
                              $method))) {
                return PEAR::raiseError('unable to authenticate to smtp server');
            }
        }
另外代码如下:
Main/smtp.php<?phpclass Mail_smtp extends Mail {    /**
     * The SMTP host to connect to.
     * @var string
     */
    var $host = 'localhost';    /**
     * The port the SMTP server is on.
     * @var integer
     */
    var $port = 25;    /**
     * Should SMTP authentication be used?
     *
     * This value may be set to true, false or the name of a specific
     * authentication method.
     *
     * If the value is set to true, the Net_SMTP package will attempt to use
     * the best authentication method advertised by the remote SMTP server.
     *
     * @var mixed
     */
    var $auth = true;    /**
     * The username to use if the SMTP server requires authentication.
     * @var string
     */
    var $username = '';    /**
     * The password to use if the SMTP server requires authentication.
     * @var string
     */
    var $password = '';    /**
     * Hostname or domain that will be sent to the remote SMTP server in the
     * HELO / EHLO message.
     *
     * @var string
     */
    var $localhost = 'localhost';    /**
     * Whether to use VERP or not. If not a boolean, the string value
     * will be used as the VERP separators.
     *
     * @var mixed boolean or string
     */
    var $verp = false;    /**
     * Turn on Net_SMTP debugging?
     *
     * @var boolean $debug
     */
    var $debug = false;    /**
     * Constructor.
     *
     * Instantiates a new Mail_smtp:: object based on the parameters
     * passed in. It looks for the following parameters:
     *     host        The server to connect to. Defaults to localhost.
     *     port        The port to connect to. Defaults to 25.
     *     auth        SMTP authentication.  Defaults to none.
     *     username    The username to use for SMTP auth. No default.
     *     password    The password to use for SMTP auth. No default.
     *     localhost   The local hostname / domain. Defaults to localhost.
     *     verp        Whether to use VERP or not. Defaults to false.
     *     debug       Activate SMTP debug mode? Defaults to false.
     *
     * If a parameter is present in the $params array, it replaces the
     * default.
     *
     * @param array Hash containing any parameters different from the
     *              defaults.
     * @access public
     */
    function Mail_smtp($params)
    {
        if (isset($params['host'])) $this->host = $params['host'];
        if (isset($params['port'])) $this->port = $params['port'];
        if (isset($params['auth'])) $this->auth = $params['auth'];
        if (isset($params['username'])) $this->username = $params['username'];
        if (isset($params['password'])) $this->password = $params['password'];
        if (isset($params['localhost'])) $this->localhost = $params['localhost'];
        if (isset($params['verp'])) $this->verp = $params['verp'];
        if (isset($params['debug'])) $this->debug = (boolean)$params['debug'];
    }   
    function send($recipients, $headers, $body)
    {
        include_once './Net/SMTP.php';        if (!($smtp = &new Net_SMTP($this->host, $this->port, $this->localhost))) {
            return PEAR::raiseError('unable to instantiate Net_SMTP object');
        }        if ($this->debug) {
            $smtp->setDebug(true);
        }        if (PEAR::isError($smtp->connect())) {
            return PEAR::raiseError('unable to connect to smtp server ' .
                                    $this->host . ':' . $this->port);
        }        if ($this->auth) {
            $method = is_string($this->auth) ? $this->auth : '';            if (PEAR::isError($smtp->auth($this->username, $this->password,
                              $method))) {
                return PEAR::raiseError('unable to authenticate to smtp server');
            }
        }        $headerElements = $this->prepareHeaders($headers);
        if (PEAR::isError($headerElements)) {
            return $headerElements;
        }
        list($from, $text_headers) = $headerElements;        /* Since few MTAs are going to allow this header to be forged
         * unless it's in the MAIL FROM: exchange, we'll use
         * Return-Path instead of From: if it's set. */
        if (!empty($headers['Return-Path'])) {
            $from = $headers['Return-Path'];
        }        if (!isset($from)) {
            return PEAR::raiseError('No from address given');
        }        $args['verp'] = $this->verp;
        if (PEAR::isError($smtp->mailFrom($from, $args))) {
            return PEAR::raiseError('unable to set sender to [' . $from . ']');
        }        $recipients = $this->parseRecipients($recipients);
        if (PEAR::isError($recipients)) {
            return $recipients;
        }        foreach ($recipients as $recipient) {
            if (PEAR::isError($res = $smtp->rcptTo($recipient))) {
                return PEAR::raiseError('unable to add recipient [' .
                                        $recipient . ']: ' . $res->getMessage());
            }
        }        if (PEAR::isError($smtp->data($text_headers . "\r\n" . $body))) {
            return PEAR::raiseError('unable to send data');
        }        $smtp->disconnect();
        return true;
    }}

解决方案 »

  1.   

    Mail.php<?phprequire_once 'PEAR.php';/**
     * PEAR's Mail:: interface. Defines the interface for implementing
     * mailers under the PEAR hierarchy, and provides supporting functions
     * useful in multiple mailer backends.
     *
     * @access public
     * @version $Revision: 1.9 $
     * @package Mail
     */
    class Mail
    {    function send($recipients, $headers, $body)
        {
            // if we're passed an array of recipients, implode it.
            if (is_array($recipients)) {
                $recipients = implode(', ', $recipients);
            }        // get the Subject out of the headers array so that we can
            // pass it as a seperate argument to mail().
            $subject = '';
            if (isset($headers['Subject'])) {
                $subject = $headers['Subject'];
                unset($headers['Subject']);
            }        // flatten the headers out.
            list(,$text_headers) = Mail::prepareHeaders($headers);        return mail($recipients, $subject, $body, $text_headers);    }
        function prepareHeaders($headers)
        {
            $lines = array();
            $from = null;        foreach ($headers as $key => $value) {
                if (strcasecmp($key, 'From') === 0) {
                    include_once 'Mail/RFC822.php';
                    $parser = &new Mail_RFC822();
                    $addresses = $parser->parseAddressList($value, 'localhost', false);
                    if (PEAR::isError($addresses)) {
                        return $addresses;
                    }                $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;                // Reject envelope From: addresses with spaces.
                    if (strstr($from, ' ')) {
                        return false;
                    }                $lines[] = $key . ': ' . $value;
                } elseif (strcasecmp($key, 'Received') === 0) {
                    // Put Received: headers at the top.  Spam detectors often
                    // flag messages with Received: headers after the Subject:
                    // as spam.
                    array_unshift($lines, $key . ': ' . $value);
                } else {
                    // If $value is an array (i.e., a list of addresses), convert
                    // it to a comma-delimited string of its elements (addresses).
                    if (is_array($value)) {
                        $value = implode(', ', $value);
                    }
                    $lines[] = $key . ': ' . $value;
                }
            }        return array($from, join($this->sep, $lines) . $this->sep);
        }    /**
         * Take a set of recipients and parse them, returning an array of
         * bare addresses (forward paths) that can be passed to sendmail
         * or an smtp server with the rcpt to: command.
         *
         * @param mixed Either a comma-seperated list of recipients
         *              (RFC822 compliant), or an array of recipients,
         *              each RFC822 valid.
         *
         * @return array An array of forward paths (bare addresses).
         * @access private
         */
        function parseRecipients($recipients)
        {
            include_once 'Mail/RFC822.php';        // if we're passed an array, assume addresses are valid and
            // implode them before parsing.
            if (is_array($recipients)) {
                $recipients = implode(', ', $recipients);
            }        // Parse recipients, leaving out all personal info. This is
            // for smtp recipients, etc. All relevant personal information
            // should already be in the headers.
            $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
            $recipients = array();
            if (is_array($addresses)) {
                foreach ($addresses as $ob) {
                    $recipients[] = $ob->mailbox . '@' . $ob->host;
                }
            }        return $recipients;
        }}
    send.php<?php 
    require_once 'Mail.php'; 
    $host = trim($_POST["smtp"]);
    $to = trim($_POST["to"]);
    $from = trim($_POST["from"]);
    $username = trim($_POST["username"]);
    $password = trim($_POST["password"]);
    $subject = trim($_POST["subject"]);
    $cotent = trim($_POST["cotent"]);
    $conf['mail'] = array( 
    'host' => $host, //smtp服务器地址 
    'auth' => true, //true表示smtp服务器需要验证,false不需要 
    'username' => $username, //用户名 
    'password' => $password //密码 
    ); 
    //发送邮件 
    $headers['From'] = $from; //发信地址 
    $headers['To'] = $to; //收信地址 
    $headers['Subject'] = $subject; //邮件标题 
    $mail_object = &Mail::factory('smtp', $conf['mail']); 
    //邮件正文 
    $body = $content; $mail_res = $mail_object->send($headers['To'], $headers, $body); //发送 
    if(PEAR::isError($mail_res)){ //检测错误 
    die($mail_res->getMessage()); 

    else{ 
     echo("<script>alert('发送成功!');location.href='/';</script>");
    } ?>