Home » Mail » Mail » Bug #6983
no codes generated for PEAR_Error
Details
| Request #6983 | no codes generated for PEAR_Error |
|---|---|
| Submitted | 2006-03-01 20:03 UTC |
| From | loikiolki at yahoo dot ca |
| Assigned | jon |
| Status | Closed |
| Package | |
| PHP Version | Irrelevant |
| OS | Windows XP |
| Roadmaps | (Not assigned) |
Comments
[2006-03-01 20:03 UTC] loikiolki at yahoo dot ca
Description:
------------
As the documentation for the send() function shows, the Mail package does not provide an error code for the PEAR_Error object returned upon any failure. This is extremely annoying to program around.
If the authentication fails, I should be able to get a specific code from the PEAR_Error object, rather than having to search for substrings within the error message. On top of this, the error messages returned are not even the ones reported in the documentation for send().
Documentation states that authentication failure returns "unable to authenticate to smtp server". What it really returns is "authentication failure [SMTP: Invalid response code received from server (code: 503, response: Authentication failed)]". Inconsistent.
Test script:
---------------
<?php
require_once 'Mail.php';
$mail = Mail::factory('smtp', array(
'auth' => true,
'username' => 'me@example.com',
'password' => 'an INVALID passwd here'
));
$headers = array(
'From' => 'me@example.com',
'To' => 'you@example.com',
'Subject' => 'Example Subject'
);
$res = $mail->send(
'you@example.com', $headers, 'Sample Body'
);
list($err, $msg) = array(
$res->getCode(), $res->getMessage()
);
// This should work
if ($err > 0) {
echo "Error!\nCode: {$err}\nMsg: {$msg}\n";
}
else {
echo "E-mail successfully sent!\n";
}
echo "\n";
// So should this
switch ($err) {
123:
echo "authentication failed: $msg\n";
break;
$some_other_int:
echo "smtp connection failed: $msg\n";
break;
default:
echo "some other error: #{$err} :: $msg\n";
}
?>
Expected result:
----------------
Error!
Code: 123
authentication failure [SMTP: Invalid response code received from server (code: 503, response: Authentication failed)]
authentication failed: authentication failure [SMTP: Invalid response code received from server (code: 503, response: Authentication failed)]
Actual result:
--------------
no error codes provided.
[2006-03-01 20:24 UTC] loikiolki at yahoo dot ca
OK, so there is a way to get at the SMTP failure code, which is what I will use. It would still be nice to get the value from the PEAR_Error->getCode() function though.
<?php
$res = $mail->send(
'you@example.com', $headers, 'Sample Body'
);
if ($res === true) {
echo "e-mail sent\n";
}
else {
preg_match('/(\d+)/', $res->getMessage(), $match);
echo "Error!\nCode: $match[0]\n" .
"Msg: {$res->getMessage()}\n";
}
?>