mailError
The code is an example of custom error handling in PHP that sends errors to an email address. It defines a custom error handling function called customError() which will be called when an error occurs. The function takes two parameters: $errno, which is the error number, and $errstr, which is the error message.
The function then uses the mail() function to send an email to the admin. The mail() function takes three parameters: the recipient email address, the subject of the email, and the message. In this case, the recipient email address is 'admin@website.com', the subject is 'Error' and the message is $errstr.
The script also sets this custom error handler function to be used with the set_error_handler() function. This function takes two parameters: the name of the function to be used as the error handler and the error level to be handled by the error handler. In this case, it is set to handle E_WARNING level errors.
After this, the code triggers an error by calling a require statement to include a file that does not exist, 'fileDoesNotExists.txt', this will throw an error because the file does not exist. The error handler function customError() will be called, it will send an email to the admin with the error message "fileDoesNotExist.txt" and also display the message 'Error: 2 fileDoesNotExist.txt' to the browser.
This method allows you to keep track of errors that occur on a live website, and can also be useful for debugging. It also allows the administrator to receive an email with the error message and take necessary actions. Please note that the mail function uses the server's local mail transfer agent to deliver the mail and it may not work in all server configurations and also it is not recommended to use this function for a large number of emails as it may overload the server.
<?php
// You can also email errors to the admin
// this is done by using the error_log() function
// the error_log() function sends an error to email
// this function will be called when an error occurs
function customError($errno, $errstr)
{
// send email to admin
mail('admin@website.com', 'Error', $errstr);
echo "Error: [$errno] $errstr";
}
// set error handler
set_error_handler("customError", E_WARNING);
// trigger error on false require
require ('fileDoesNotExists.txt'); // this will throw an error because the file does not exist