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.