Amateur Protagonist Alister Sneddon

I once almost won a procrastinating contest, but I turned up.


PHP Make Or Die Send An Email

10th January 2015
By Alister Sneddon

Let's get this straight out there right this now second. If you are using the die function in your code you are a terrible person. Always handle your errors and end things gracefully.

That said sometimes it simply does not matter or you do not have the time or maybe you have a good reason for using die. FYI I have never outside of local testing found a good reason for using die.

Now this site uses die to handle really big database errors or any issue which could be a threat to the system. The reason for this is to protect the system. I use CloudFlare to protect against my server buckling under pressure and I use die to protect against single user mistakes or servers which I depend on not being available.

Again die is never the solution. Never.

However when you do use die it is nice to know that you had to use it. For a long time this site was using die and that was the end of it. I never had a need or care beyond not spitting out raw errors.

For a very long time I also had a memory leak issue relating to my database on this site. The fix was simple but I had to know it happened. My site uptime is only checked every 5 minutes and it needs to fail two times in a row. Basically my database would have to be offline for 10 minutes before I am emailed.

So I made a very simple change so I would be emailed what went wrong. This is an extremely basic function made to replace die. You do not want to copy the function and start using it. Make some changes, figure what you need to see and what matters to you.

Here is the code, after this block of code I will quick talk about each section and then some examples.

function die_and_email($body) {
    $mailto = "MYEMAIL";
    $mailsubj = "Website Used Die!";
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $headers .= "From: MYEMAIL\r\n";
    
    $mailbody = "<html><body><h3>Your website just used Die!</h3>\n";
    $mailbody .= "<p>The die was triggered and this email has been sent.</p>";
    $mailbody .= "<p>Die was stamped at: " . date("Y-m-d H:i:s") . "</p>";
    $mailbody .= "<p>The following message was passed in:<br />";
    $mailbody .= $body . "</p>";
    $mailbody .= "<p><small>All the best,<br />Alister Sneddon</small></p></body></html>";

    // Send the mail
    mail($mailto, $mailsubj, $mailbody, $headers); 
    die('<h1>Sorry!</h1><p>This is real bad, I have just sent an email to myself.<br />You should leave.</p>');
}

So this code would be used in place of your standard "or die(Helpful message');" The first variable would be who gets the email. The second variable is the subject line to use. Then we have the headers. The first two headers make it a HTML email. The third header is who the email comes from, I recommend you make this the same as the address which will receive the email for now.

Then there is the mailbody variable. This is simple HTML with some flavour text. The email will pass in the message you write as well as the time stamp in MySQL format. This is super helpful for working out the accurate time of the issue.

Then we send the email and display a HTML die.

This is a pretty basic setup so let me show you its usage in the wild.

mysql_connect($host, $username, $password)
	or die_and_email('Failed to connect: mysql_connect($host: ' . $host
		. ', $username: ' . $username . ', $password: ' . $password . ')');

In this case we simply respond with the function which has failed and show what parameters we were passing in. This is handy for situations where the variables you pass in might change or vary depending on system (so you can pick up the wrong system has the wrong version.)

Here is another example in which we pass in a simple string as reference. There is no or in this situation, the user has completed an action to which we simply want to die.

die_and_email('Test script attempted to run.');

Finally you can use this to give basic information as to who caused the problem

die_and_email('User from the IP: <strong>' . $_SERVER['REMOTE_ADDR'] 
	. '</strong> loaded <strong>test.php</strong>.');

Hopefully this has given you some ways to improve your visibility on errors. If you have access to your servers error log then I suggest you change the script so that before the die you do "error_log($body);" that way you will pass the same message from your email into your error log which is automatically time stamped for you.

You filthy comment whore, you love it don't you?
Lets not be forgetting to +1 it now... I am tracking your IP...