For this edition of Quick Codes I am sharing how to send an email whenever someone lands on your 404 File Not Found error page. I found this on CSS-Tricks.com’s 404 Best Practices post and had to share it. Just to clarify this code has not been vetted to ensure security or that it works perfectly, but sometimes it gets the job done just fine. This code is PHP, but the idea could be adapted to any language. For more good ideas don’t forget to check out my post Better 404 error pages by design.
mail("name@domain.com", "404 report", $_SERVER['REQUEST_URI'], "From: example@domain.comn");
UPDATE:
Thanks to a recent comment by Thomas Scholz, I added the addslashes() function to escape any funny stuff inside the request url.
mail("name@domain.com", "404 report", $_SERVER['HTTP_HOST'] . addslashes($_SERVER['REQUEST_URI']), "From: example@domain.comn");
And I also found a good piece of code on codex.wordpress.org which is an excellent resource. View the full page with example code here: Creating an Error 404 Page « WordPress Codex



