Send Email Notifications for Broken Images Using MooTools AJAX

By  on  

One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible. I've created a MooTools / PHP script that listens for image errors, triggers an AJAX call to a PHP script, and that PHP script sends an email letting me know about the problem.

The MooTools JavaScript

window.addEvent('domready',function() {
	$$('img').addEvent('error',function() {
		var notification = new Request({
			url: 'ajax-image-error.php',
			method: 'post',
			data: {
				'image': this.get('src'),
				'page': window.location.href
			}
		}).send();
	});
});

You'll notice that I listen for the error event. You'll also notice that I only send two parameters: the image path and the page the broken image is living (or dying) on.

The PHP

if(isset($_POST['image']))
{
	$to = 'errors@yourdomain.com';
	$from = 'automailer@yourdomain.com';
	$subject = 'Broken Image';
	$content = "The website is signaling a broken image!\n\nBroken Image Path:  ".stripslashes($_POST['image'])."\n\nReferenced on Page:  ".stripslashes($_POST['page']);
	$result = mail($to,$subject,$content,'From: '.$from."\r\n");
	die($result);
}

Nothing special within the email -- just the parameters we put into the AJAX call. This bare email will provide us enough information to solve the problem.

One addition to the MooTools script could be to remove the image from the page so that the user doesn't notice the problem. Have any suggestions? Share them!

Recent Features

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

  • By
    Introducing MooTools NextPrev

    One thing I love doing is duplicating OS functionalities. One of the things your OS allows you to do easily is move from one item to another. Most of the time you're simply trying to get to the next or the previous item.

Discussion

  1. I have only one question — why do it client side? I mean, since you can make a custom e404 page (or any error) you can make the error page to handle the emails.

    Further more to avoid the “missing image” issue, you could return a 1×1 transparent gif / jpg / png to e404 requests for images. Or to make it even more advance, you could customize the return image depending on the path of the image, say 100×75 jpg if the request matches /images/thumbs.

    Of course the above mentioned approach reqiers more code.

    On a side note — is this post related to this article?

  2. @Иван: Note that this is about broken images, not broken pages. Good idea about a custom missing images image.

  3. Oh that is perfect! Nice and simple.

  4. anon

    why not just grep your logfiles? iif the page with the broken image gets dugg then your going to get a bunch of emails.

  5. I think that this article (and Benajmin Sterling’s comment) hits it on the head – this is a simple, interesting approach to an issue, and can be applied more than one way.

    This method? Anyone can handle implementing (okay, anyone with some basic code knowledge) – grepping your error log or doing custom 404 pages isn’t exactly something anyone can do.

    That, and this is a very simple method to apply to pretty much any loaded method on a page (SWF, JS, CSS….) very cool idea.

  6. umo

    Great post, never thought about this. The concept is great, however I must agree with “anon” that an active site might spanban the server smtp.

    Why not complete the email code with a seperate log entry, or a database entry? With a database entry, just one email could be sent at the first broken image hit, and the image path could serve as an identifier for the log entry.

  7. Seth

    I love this site! Not because I necessarly have a use for this myself, but I didn’t know about the error event. That is huge! Keep it up

  8. Good script!
    But a version with jquery it’s possible?
    Thanks,Mte90

  9. Interesting, it works with http://dummysite/image.jpg but fails with image.jpg

    scratching my head to figure what’s going on….

  10. Superb Post. Better then the simillar post I seen couple of days ago online. Keep up the good work.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!