Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEAR Mail not sending but also not reporting error

Tags:

php

email

pear

I am trying to send an email via PHP using PEAR Mail but although the page reports that the mail has been sent, it never arrives (I'm sending it to myself for testing).

I have been researching how to handle errors and if I turn strict reporting on, I get about a half dozen reports of these:

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in Blah Blah Blah! on line 450

Strict Standards: Non-static method PEAR::raiseError() should not be called statically, assuming $this from incompatible context in Blah Blah Blah! on line 451

In my reading I am told that these errors do not stop the script from succeeding and that most people just leave strict reporting off, however in my case the script doesn't work.

I have tried the following methods to catch an error...

try {
$host = "ssl://mail.example.com";  
$port = 25;
$auth = true; // turn on SMTP authentication  
$username = "[email protected]"; // SMTP username  
$password = "password"; // SMTP password 

$mail = Mail::factory('smtp', 
        array('host'=>$host,'port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password));
$mail->send($to,$headers,$message);
} catch (Exception $e) {
echo "Exception: " . $e->getMessage();
}
echo "Message Successfully Sent!";

And also without the try catch and simply using...

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}

In both cases the page reports "Email successfully sent!" but the mail doesn't arrive. If I purposefully feed in incorrect user and password or a fictional mail server, no error is reported.

How do I error check in this case and why would the script still run if I give it an obvious error?

Thanks Greg

like image 493
Barbs Avatar asked Feb 15 '23 05:02

Barbs


1 Answers

Dagon,

Thanks for pointing me in the right direction. Upon further searching I found how to set $params['debug'] and that lead me to the source of the problem.

So the answer for those struggling to find a way to debug their mail sending attempts is...

$params = array('debug'=>true,'host'=>$host,'port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password);
$mail = Mail::factory('smtp', $params);
$mail->send($to,$headers,$message);

This will display the mail connection responses and all the content being sent to the mail server for debugging.

like image 133
Barbs Avatar answered Feb 24 '23 05:02

Barbs