简体   繁体   English

PHP Pear Mail无法用作对象

[英]PHP Pear Mail not working as an object

I have created a class that will be used to send email but when I integrate the Pear components it fails. 我创建了一个用于发送电子邮件的类,但是当我集成Pear组件时,它会失败。

"Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]" “无法连接到本地主机:25 [SMTP:无法连接套接字:连接被拒绝(代码:-1,响应:)]”

The procedural version runs perfectly, but not the OOP version. 程序版本可以完美运行,但OOP版本却不能。

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. 不知道在构造中是否需要Mail.php是最佳实践,但这是我发现在此之前修复另一个连接错误的唯一方法。

That's because you're using $_host, $_username and $_password in your Mail::factory call. 这是因为您在Mail :: factory调用中使用了$ _host,$ _ username和$ _password。 I think you mean $this->_host, $this->_username and $this->_password – Colin Morelli 我认为您的意思是$ this-> _ host,$ this-> _ username和$ this-> _ password – Colin Morelli

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM