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条回答 默认 最新

  • dongxuan2015 2013-03-07 18:56
    关注

    That's because you're using $_host, $_username and $_password in your Mail::factory call. I think you mean $this->_host, $this->_username and $this->_password – Colin Morelli

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题