Send Email for HTTP Error Codes 401, 403, 404, 500 using Perl CGI-bin
Ever wondered how long one of your websites has been down, but you didn't check your error_log file? There is an easy way to be alerted when there is an error on your website, such as 404 Not Found, 500 Internal Server Error, and others. I have made a simple CGI script using Perl that sends an email to the webmaster saying what page caused the error, what the error code is, and what the visitor's IP address is. I call it sendmail.pl.
You can download sendmail.pl
here or copy and the code below and
paste it in your own file.
#!/usr/bin/perl -w
use CGI qw/:standard/;
# sendEmail function
# used from code found on http://perl.about.com/od/email/a/perlemailsub.htm
# modified on August 8, 2008 by Mike Rodarte
# http://www.mts7.com
#
# This function is used to send email to an administrator of the given
# website.
################################################################################
# configure the options below #
# #
my $error_page = "/error.html"; #
my $admin_email = "administrator\@yourdomain.com"; #
# #
# the from email address is set to serveradmin@servername.com #
# the subject is set to Error on site www.servername.com #
# #
# do not configure code below this line #
# unless you know what you're doing #
################################################################################
# send the email with the current http status and the admin email address
sendEmail($ENV{REDIRECT_STATUS}, $admin_email);
# redirect the user to the supplied error page
print redirect('http://'.$ENV{SERVER_NAME}.$error_page);
sub sendEmail
{
# get parameters from function
my ($error, $to_email) = @_;
# these variables are used in the message
my $server = $ENV{SERVER_NAME};
my $uri = $ENV{REQUEST_URI};
my $path = "http://".$server.$uri;
my $ip = $ENV{REMOTE_ADDR};
# find the root domain name of the server (no subdomains like www)
my $last_dot = rindex($server, ".");
my $previous_dot = rindex($server, ".", $last_dot-1);
my $small_string = substr($server, $previous_dot + 1);
# message to send
my $message = "There was an error ".$error." when ".$ip.
" tried to access ".$path;
# to email from configured option above
my $to = $to_email;
# you can change this email address to whatever you want
# from email is serveradmin @ server root domain name
my $from = "serveradmin\@".$small_string;
# subject for email
my $subject = "Error on Site ".$server;
# path to sendmail
my $sendmail = '/usr/lib/sendmail';
################################################################################
# the code below sends the email #
# do not edit unless you know what you're doing #
################################################################################
# send the email
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}
Take your copy of sendmail.pl and upload it to your cgi-bin directory. Use FTP, SSH, or anything similar to change it to an executable file. For example, in SSH you can type "chmod +x sendmail.pl" without the quotes. It sends an email and then forwards to the error document you specify. The default is error.html.
Once this is taken care of, modify .htaccess in your HTTP root directory (public_html), or create it if it does not exist. You can download a sample of .htaccess here. Be sure to save it as "All Files" for it to work properly. Inside .htaccess, place the following lines in it:
- ErrorDocument 401 /cgi-bin/sendmail.pl
- ErrorDocument 403 /cgi-bin/sendmail.pl
- ErrorDocument 404 /cgi-bin/sendmail.pl
- ErrorDocument 500 /cgi-bin/sendmail.pl
You can use whatever error codes you choose, but these are what I am using in this example. Using these codes, all Unauthorized, Forbidden, Not Found, and Internal Server Error requests will execute the script.
Be sure to have your error.html (or similar) document on your server because the script will forward to it after the script sends the email. You can configure the error document's name in the Perl script, as well as the webmaster's email address. To see my error.html, click here.
To review, you should have 3 files on your web server: cgi-bin/sendmail.pl, .htaccess, and error.html. All of these files work together to produce the webmaster's alert and keep the visitors from seeing the default error documents.
If you have questions or comments about this script, please use the live chat feature on the website here or send an email to the address on the contact page. This is not the only way to alert the webmaster, but it works well. Feel free to recommend revisions.
