Search code examples
phpemailpear

Error with PEAR based email class


I'm trying to get an email class based on PHP's PEAR library to work, but I constantly stumble across this message: Fatal error: Call to a member function send() on a non-object in /PATH/email.php on line 42. I've tried a few different things and they haven't resolved the problem in the following code:

<?php

require_once 'Mail.php';
require_once './config/config.php';

class Email
{
    var $smtp_host;
    var $smtp_port;
    var $smtp_user;
    var $smtp_pass;
    var $from;
    var $smtp_conn;

    function Email($init_smtp_host=SMTP_HOST, $init_smtp_port=SMTP_PORT, $init_smtp_user=SMTP_USER, $init_smtp_pass=SMTP_PASS, $init_from_name='admin', $init_from_email=SMTP_USER)
    {
        $smtp_host = $init_smtp_host;
        $smtp_port = $init_smtp_port;
        $smtp_user = $init_smtp_user;
        $smtp_pass = $init_smtp_pass;
        $from = $init_from_name . ' <' . $init_from_email . '>';

    $smtp_conn =& Mail::factory('smtp', 
                        array(  'host' => $this->smtp_host,
                                'port' => $this->smtp_port,
                                'auth' => TRUE,
                                'username' => $this->smtp_user,
                                'password' => $this->smtp_pass));
}

function send($to_name, $to_email, $subject, $message)
{
    $to = $to_name . ' <' . $to_email . '>';

    $header = array('From' => $this->from, 'To' => $to, 'Subject' => $subject);

    $mail = $this->smtp_conn->send($to, $header, $message);

    if (PEAR::isError($mail))
        return $mail->getMessage();
    else
        return 0; 
}
}

Am I doing something wrong on $mail = $this->smtp_conn->send($to, $header, $message); (where the error is being displayed)? Thanks


Solution

  • What's the type of the $smtp_conn? If the factory fails, it'd return something other than a Mail object, like perhaps a PEAR::Error.