简体   繁体   English

通过电子邮件php发送错误消息

[英]Send an Error Message by E-Mail php

I want to get a mail if any error occurs in my php apps. 如果我的php应用程序中发生任何错误,我想收到一封邮件。 I found an example on https://www.w3schools.com/php/php_error.asp : 我在https://www.w3schools.com/php/php_error.asp上找到了一个示例:

<?php
    //error handler function
function customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr<br>";
  echo "Webmaster has been notified";
  error_log("Error: [$errno] $errstr",1,
  "uwe.nachname@gmail.com","From: webmaster@example.com");
}
//set error handler
set_error_handler("customError",E_USER_WARNING);
//trigger error
$test=2;
if ($test>=1) {
  trigger_error("Value must be 1 or below",E_USER_WARNING);
}
?>

This is working, but when I change it for all errors not: 这是可行的,但是当我为所有错误更改它时,则不会:

<?php
    error_reporting(E_ALL);
ini_set('display_errors', 'Off');
//error handler function
function customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr<br>";
  echo "Webmaster has been notified";
  error_log("Error: [$errno] $errstr",1,
  "uwe.nachname@gmail.com","From: webmaster@example.com");
}
//set error handler
set_error_handler("customError",E_ALL);
  echo 'now the error: ';
  echo gibtsnicht();
?>

What can I do to get a mail for any error?? 如果出现任何错误,我该怎么办? Thanks and greetings from Austria Uwe 来自奥地利乌威的感谢和问候

The issue is that this is not triggering an error, but an exception, so the exception has to be caught and handled. 问题在于这不是触发错误,而是引发异常,因此必须捕获并处理该异常。

<?php
function customException($ex) {
  echo "<b>Exception:</b> ".$ex->getMessage()." Line: ".$ex->getLine() ."<br>";
  error_log("Exception: " .$ex->getMessage()." File: ".$ex->getFile()." Line: ".$ex->getLine(),1,
  "uwe.nachname@gmail.com","From: webmaster@example.com");
  echo "Webmaster has been notified";
}

set_exception_handler("customException"); 
  echo 'now the exception: <br>';
  echo gibtsnicht();
?>

Check out Exception 签出异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM