我创建了一个用于发送电子邮件的类,但是当我集成Pear组件时,它失败了。
“无法连接到localhost:25 [SMTP:无法连接套接字:连接被拒绝(代码:-1,响应:)]”
程序版本运行完美,但不是OOP版本。
可以找到程序版本模板here
这是我的班级
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;
}
}
}
不确定在构造中是否需要Mail.php是最佳做法,但这是我发现在此之前修复另一个连接错误的唯一方法。
答案 0 :(得分:0)
那是因为你在Mail :: factory调用中使用$ _host,$ _username和$ _password。我认为你的意思是$ this-> _host,$ this-> _username和$ this-> _password - Colin Morelli