Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Error Trapping with Mail() and PHP

Error Trapping with Mail() and PHP
Thread Tools
Junior Member
Join Date: May 2003
Status: Offline
Reply With Quote
Mar 11, 2004, 02:02 PM
 
Hey guys,

Recently, my server has stopped sending e-mail with the PHP mail() function. Does anyone know how to trap what the error might be? As of right now, my code looks like this:

$user_email = "jon@jmarus.com";
$header = "From: Jon Marus <jon@jmarus.com>\r\n";
$header .= "Reply-To: Jon Marus <jon@jmarus.com>\r\n";
$subject = "Test E-mail";
$message = "This is a test e-mail.";


if (mail($user_email, $subject, $message, $header)) {
echo "<p>E-mail sent.</p>";
} else {
echo "<p>There was a problem with the server. Please try again later.</p>";
}

And it just returns: There was a problem with the server. Please try again later.

But I need a better error message to report to my webhost, who is getting extremely upset with me, because he says everything is fine.

Thanks!
Jon Marus
www.jmarus.com - kicks by JM
     
Senior User
Join Date: Nov 2003
Location: Middle of the street
Status: Offline
Reply With Quote
Mar 12, 2004, 02:20 PM
 
I found this while googleing (sp?); the relevant text is quoted below for your convenince:

This script will alert the webmaster quickly if an error occurs on a PHP Web page by emailing the person details of the error. This is very useful for Web sites with part-time webmasters and large ones where not every page can be tested by the webmaster.

You need to define 2 variables, $gAdminMail which is the email address of the webmaster, and $gErrorPage which is the page to redirect to if an error occurs.

It makes use of the PHP4 feature set_error_handler, which lets PHP call a user-defined function when an error occurs.

You need to have mail() properly configured on your system.

Place the following code at the beginning of your PHP files:

[php]/* decide what errors to report */
error_reporting (E_ERROR | E_WARNING| E_NOTICE);

$gAdminMail = 'webmaster@dot.com'; $gErrorPage = '/error.htm';

/* error handler function */
function MailErrorHandler($errno, $errstr, $errfile='?', $errline= '?') {
global $gAdminMail, $gErrorPage;
if (($errno & error_reporting()) == 0) /* ignore error */
return;
$err = '';
switch($errno) {
case E_ERROR:
$err = 'FATAL';
break;
case E_WARNING:
$err = 'ERROR';
break;
case E_NOTICE:
return; /* E_NOTICE is normally harmless */
}
mail($gAdminMail, "PHP: $errfile, $errline", "$errfile, Line $errline\n$err($errno)\n$errstr");
print '<META HTTP-EQUIV="Refresh" CONTENT="0;url='.$gErrorPage.'">';
die();
}
set_error_handler('MailErrorHandler');
[/php]
Hope this helps; as I tried your code on my server and it worked perfectly. You can also do a phpinfo() on your webhost and see if they have the smtp functionality enabled, a long shot, I know; but it might work.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:01 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2