我创建了一个用于发送电子邮件的类,但是当我集成 Pear 组件时它失败了。
“无法连接到本地主机:25 [SMTP:无法连接套接字:连接被拒绝(代码:-1,响应:)]”
程序版本运行完美,但 OOP 版本不运行。
程序版本模板可以在这里找到
这是我的课
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 是否是最佳实践,但这是我发现在此之前修复另一个连接错误的唯一方法。