Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » RESOLVED: Send notification email to administrator on Error 404 Page

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2322 Views
  • DJMatus23
    Community Member
    11 Posts

    RESOLVED: Send notification email to administrator on Error 404 Page Link to this post


    I'm about to receive a freshly developed Silverstripe site, unfortunately the developers don't feel it is in the scope of the original project description to have an email sent to the site administrator on every 404 error...so I'm doing it myself, which is fine. My problem is, I can use Debug::send_errors_to('me') to have HTTP 500 sent to me, but not 404s. I don't want to hack the site too much, so I'm hoping there is a nice little directive that means I get a nice little:

    404 Error
    Source: http://www.whatever.com/FrontPage
    Destination: http://www.whatever.com/MissingPage

    Can anyone help me, or will I need to hack the published 404 page via .htaccess and PHP (or something similar) to trigger the email? If I need to hack, if anyone has done it before, would appreciate a copy and paste of the .htaccess and PHP bits, to save me some time.

    Also, is there a way to change the From address of the error from send_errors_to() other than hacking the Debug.php file?


    P.S. For another 404 question, see: http://silverstripe.org/customising-the-cms/show/259763

  • DJMatus23
    Community Member
    11 Posts

    Re: RESOLVED: Send notification email to administrator on Error 404 Page Link to this post

    For anyone who finds themselves looking at this in the future, the solution I ended up with was to edit the following file:


    and add the following text to it (I just put it after the $Content line, but there may be better places for it):

    document.write("\<script type=\"text\/javascript\" src=\"\/error_logging_script.php?err_code="+$ErrorCode+"&from_uri=" + document.referrer + "&to_uri=" + document.URL + "\"\>\<\/script\>");

    Then I created a simple PHP script which took those three GET parameters "err_code", "from_uri" and "to_uri", saved it as error_logging_script.php in the HTTPROOT with the following:

    $ip = getenv ("REMOTE_ADDR");

    $today = date("D M j Y g:i:s a T");

    $message = "Date: $today
    IP: $ip
    Missing URI: ".$_GET["to_uri"]."
    User Agent = $httpagent
    Referrer = ".$_GET["from_uri"];

    $to = "me@me.com";
    $subject = "www.mysite.com Error ".$_GET["err_code"];
    $from = "From: webmaster@mysite.com\r\n";

    mail($to, $subject, $message, $from);

    For brevity, I've taken out all of the additional validation and filtering (to take out bots' failed attempts to hack the site, etc).

    It works well (i.e. after 2-3 days use it hasn't missed anything yet), and involves no hacking so it should remain compatible with SS upgrades.

    You could also add some logging code, etc., or anything you like really - the reason I put the code just after the $Content line, was because I originally planned to have the PHP print some kind of "Email sent" type text, then realised that was actually a bit silly. I will be adding some code to redirect all 404s back to the front page of the site though, as per my other question - but I'll put that code into the response to that question when I get it done.

    Ronan 2.0

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.