Tuesday, 15 January 2019

php: set_error_handler causes Non-static method Mail::factory() should not be called statically

I have the following mail sending function:

function SendMailUtf8($to, $subject, $message) {
    require_once "Mail.php";

    $from = "mydomain <myuser@mydomain.com>";

    $host = "mydomain.com";
    $username = "myuser";
    $password = "mypassword";

    $headers = array ('From' => $from, 'To' => $to, 'Content-Type' => 'text/html; charset=UTF-8', 'X-Mailer' => 'PHP/'.phpversion(), 'Reply-To' => 'myuser@mydomain.com', 'Subject' => '=?UTF-8?B?'.base64_encode($subject).'?=');
    //this line is where the error is thrown
    $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));

    $mail = $smtp->send($to, $headers, $message);

    if (PEAR::isError($mail)) {
        return "Error: Mail not sent. Message: " . $mail->getMessage();
    } else {
        return "OK";
    }       
}

This function gets called properly without any error from everywhere EXCEPT for pages where I have set my own error handling function:

function Func_ErrorHandler($errno, $errstr, $errfile, $errline, array $errcontext) {
    if (error_reporting() == 0) {
        return false;
    }
    if ($errno > 0) {
        throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
}

with the following error message:

Uncaught exception 'ErrorException' with message 'Non-static method Mail::factory() should not be called statically

example code that fails:

set_error_handler("Func_ErrorHandler");
$MAILto      = "info@mydomain.com";
$MAILsubject = "subject";
$MAILmessage = "message";

$sRet = SendMailUtf8($MAILto, $MAILsubject, $MAILmessage);

If I comment out the set_error_hanlder() line, there is no problem. Why is this happening and how can I solve it?

Thank you in advance.



from php: set_error_handler causes Non-static method Mail::factory() should not be called statically

No comments:

Post a Comment