dongmacuo1193
2013-03-06 18:08
采纳率: 0%
浏览 31
已采纳

PHP Pear Mail不作为对象工作

I have created a class that will be used to send email but when I integrate the Pear components it fails.

"Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]"

The procedural version runs perfectly, but not the OOP version.

procedural version template can be found here

Here is my class

class Email{

    private $_from;
    private $_to;
    private $_subject;
    private $_body;
    private $_headers;

    # final
    private $_host = "ssl://smtp.gmail.com:465";
    private $_username = "email@mysite.com";
    private $_password = "mypass";

    function __construct()
    {
        require_once("Mail.php");
    }

    public function setup($from,$to,$subject,$body)
    {
        $this->_from    = $from;
        $this->_to      = $to;
        $this->_subject = $subject;
        $this->_body    = $body;

    }
    public function send()
    {
        if(isset($this->_from) && isset($this->_to) && isset($this->_subject) && isset($this->_body))
        {               
            $this->_headers = array ('From' => $this->_from, 'To' => $this->_to,'Subject' => $this->_subject);
            $smtp = Mail::factory('smtp',array ('host' => $_host,
            'auth' => true,
            'username' => $_username,
            'password' => $_password));

            $mail = $smtp->send($this->_to, $this->_headers, $this->_body);

            if (PEAR::isError($mail)) {
              $_POST['mail_error'] = $mail->getMessage(); 
              return false;
            } else {
              return true;
            }
        }
        else
        {
            $_POST['mail_error'] = "missing a required arguement";
            return false;
        }
    }
}

not sure if requiring Mail.php in the construct is best practice but it was the only way I found to fix another connection error before this one.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题