ASPAlliance ASP Kitchen  
Search: Go  

ASP Kitchen: Classic ASP Articles: An ASP Error Report Emailer

An ASP Error Report Emailer Function

Introduction

Email Error Report DetailsWhen you build websites for customers, it's often very difficult to find out the source of errors. Often the difficulties are mostly down to inaccurate error reports. For example, an error of "the page shows an error when I log in to the job search page" is a lot more difficult to fix than "the page shows a Microsoft VBScript runtime error number 500 (Variable is undefined) when I access job_search_login.asp".

A possible solution to the problem is to display friendly errors on the page. However, this  looks unprofessional when the website users encounter them. A better solution is, therefore, to incorporate an error emailing script within your ASP pages. The SendErrorEmail function described on this page emails a comprehensive error report. A sample email error report is shown in the image on the left hand side of this page. The email includes details of what caused the error, together with the details of Application and Session objects, and the contents of the QueryString, Form and Server Variables collections.

Using the function

The SendErrorEmail function should be called from any ASP pages where errors are likely to occur. It is also useful to add the function to an include file that would allow the function to be used from all pages on a site. Further details of how to use include files is available on ASPAlliance.com.

Warning!

Warning!

Be aware that the email error reports contain sensitive details about web server and website configuration, so be careful about who receives the emails and who you forward them onto.

The SendErrorEmail function has four arguments:

  • ErrorType. This is a user defined parameter, and can be used to report on the type of error (e.g. ASP error, Data access error etc.)
  • ErrorSource. The source of the error, most usually obtained from the Source property of the VBScript Err object.
  • ErrorNumber. The number of the error, most usually obtained from the Number property of the VBScript Err object.
  • ErrorDescription. A description of the error, most usually obtained from the Description property of the VBScript Err object.

Note that before the function is used, as little customisation is required. There are three areas of the script that need attention:

  • The script contains a number of constants that control who gets the emailed error reports, together with the name of the mail server which will send the emails.
  • If you disable session state on your ASP pages by using the @ENABLESESSIONSTATE = False directive, then you must comment out the parts of the script that email the details of the session object. Sadly, with classic ASP there appears to be no way of automatically determining if session support has been disabled.
  • The script has been configured to send email using the ASPMail server component from ServerObjects Inc., so if you send email using a different component a few minor changes will be required. Instructions for using the script with CDONTS are further down this page.

Once you have configured the script, it can be used using something like the following:

If Err.Number <> 0 Then
    SendErrorEmail "ASP Error", Err.Source, Err.Number, Err.Description
End If

Don't forget that the first line of your ASP page must be:

<%On Error Resume Next%>

If you don't include this then your ASP page will stop processing when it encounters an ASP error and will not therefore send the email error report.

The SendErrorEmail function

The SendErrorEmail VBScript function below can be used to send an error email and report problems with an ASP page. Details of how to use the function are here. The function can also be viewed as a text file (easier for cutting and pasting).

<%
'Function to send an alert email
'Script from http://www.aspalliance.com/brettb/ErrorReportEmailer.asp
'Parameters used are:
' ErrorType = The type of error (e.g. "ASP Error")
' ErrorSource= Error source
' ErrorNumber = Error number
' ErrorDescription= Error description
'
'Changes required if you wish to use this script:
'
'1. Change the constant declarations so the email goes to you!
'
'2. If you disable session state then you must comment out the
' part of the script that extracts the details of the Session object
'
'3. If you want to use a mail sending object other than ASPMail you need to
' alter the mail sending part of the script

Function SendErrorEmail(ErrorType, ErrorSource, ErrorNumber, ErrorDescription)

   On Error Resume Next

  
'Declare variables
    Dim HTML 'The HTML to send in the email
    Dim CollectionItem
    Dim iNumber
    Dim myMail 'Mail Server Component
    Dim QS
    Dim RF

   
'Transfer the contents of the QueryString and Form collections to variables
    Set QS = Request.QueryString
    Set RF = Request.Form

   
'Declare constants. YOU MUST CHANGE THESE WHEN USING THE SCRIPT ON YOUR OWN SITE
    Const MAIL_FROM_NAME = "MY WEBSERVER ERROR HANDLER" 'Name of email sender
    Const MAIL_FROM_EMAIL = "someone@someserver.com" 'Email address of email sender
    Const MAIL_TO_NAME = "MR. WEB DEVELOPER" 'Name of email recipient
    Const MAIL_TO_EMAIL = "someoneelse@someotherserver.com" 'Email address of email recipient
    Const MAIL_SUBJECT = "My Webserver Error Report" 'Title of error report
    Const MAIL_HOST = "smtp.someisp.com" 'Address of the host used to send the mail

   
'Generate the top part of the error report
    HTML = "<!DOCTYPE HTML PUBLIC""-//IETF//DTD HTML//EN"">"
    HTML = HTML & "<html>"
    HTML = HTML & "<head>"
    HTML = HTML & "<title>" & MAIL_SUBJECT & "</title>"
    HTML = HTML & "</head>"
    HTML = HTML & "<body bgcolor=""FFFFFF"">"
    HTML = HTML & "<p><font size =""3"" face=""Century Gothic, Arial"">"
    HTML = HTML & "<b>" & MAIL_SUBJECT & "</b><br>"
    HTML = HTML & "Error Report Generated: <FONT COLOR=""#3333FF"">" & FormatDateTime(now(), vbLongDate) & ", " & FormatDateTime(now(), vbLongTime) & "</font><br>"
    HTML = HTML & "<hr>"

   
'Generate the error report general description
    HTML = HTML & "<b>Details:</b><br>"
    HTML = HTML & "Error In Page: <FONT COLOR=""#FF3333"">" & Request.ServerVariables("PATH_INFO") & "</FONT><BR>"
    HTML = HTML & "Error Type: <FONT COLOR=""#FF3333"">" & ErrorType & "</FONT><BR>"
    HTML = HTML & "Error Source: <FONT COLOR=""#FF3333"">" & ErrorSource & "</FONT><BR>"
    HTML = HTML & "Error Number: <FONT COLOR=""#FF3333"">" & ErrorNumber & "</FONT><BR>"
    HTML = HTML & "Error Description: <FONT COLOR=""#FF3333"">" & ErrorDescription & "</FONT><BR>"
    HTML = HTML & "<hr>"

   
'Report the contents of the QueryString collection
    HTML = HTML & "<b>QueryString Collection:</b><br>"
    If QS.Count > 0 Then
    For Each CollectionItem In QS
            HTML = HTML & CollectionItem & " : <FONT COLOR=""#3333FF"">" & QS(CollectionItem) & "</FONT><br>"
    Next
    Else
    HTML = HTML & "<FONT COLOR=""#FF3333"">The QueryString collection is empty</FONT><br>"
    End If

    HTML = HTML & "<hr>"

  
'Report the contents of the Form collection
    HTML = HTML & "<b>Form Collection:</b><br>"

    If RF.Count > 0 Then
    For Each CollectionItem In RF
            HTML = HTML & CollectionItem & " : <FONT COLOR=""#FF3333"">" & RF(CollectionItem) & "</FONT><br>"
    Next
    Else
    HTML = HTML & "<FONT COLOR=""#3333FF"">The Form collection is empty</FONT><br>"
    End If

    HTML = HTML & "<hr>"

  
'Report the Server object properties
    HTML = HTML & "<b>Server Settings:</b><br>"
    HTML = HTML & "ScriptTimeout: <FONT COLOR=""#FF3333"">" & Server.ScriptTimeout & "</FONT><BR>"

    HTML = HTML & "<hr>"

   
'Report the Session object properties and the contents of the Session collection
    '
IMPORTANT: If you have disabled Sessions either in IIS or
    '
by use of the @ENABLESESSIONSTATE = FALSE directive then you MUST comment out this section
    HTML = HTML & "<b>Session Settings:</b><br>"
    HTML = HTML & "CodePage: <FONT COLOR=""#FF3333"">" & Session.CodePage & "</FONT><BR>"
    HTML = HTML & "LCID: <FONT COLOR=""#FF3333"">" & Session.LCID & "</FONT><BR>"
    HTML = HTML & "SessionID: <FONT COLOR=""#FF3333"">" & Session.SessionID & "</FONT><BR>"
    HTML = HTML & "Timeout: <FONT COLOR=""#FF3333"">" & Session.TimeOut & "</FONT><BR>"

    HTML = HTML & "<hr>"

    HTML = HTML & "<b>Session Collection:</b><br>"

    For iNumber = 1 To Session.Contents.Count
        If IsObject(Session.Contents(iNumber)) Then
            HTML = HTML & Session.Contents.Key(iNumber) & "<FONT COLOR=""#3333FF"">[Object]</FONT><BR>"
        Else
            If IsArray(Session.Contents(iNumber)) Then
                HTML = HTML & Session.Contents.Key(iNumber) & "<FONT COLOR=""#3333FF"">[Array]</FONT><BR>"
            Else
                HTML = HTML & Session.Contents.Key(iNumber) & ": <FONT COLOR=""#3333FF"">" & Session.Contents(iNumber) & "</FONT><BR>"
            End If
        End If
    Next

    HTML = HTML & "<hr>"

  
'Report the contents of the Application collection
    HTML = HTML & "<b>Application Collection:</b><br>"

    For iNumber = 1 To Application.Contents.Count
        If IsObject(Application.Contents(iNumber)) Then
            HTML = HTML & Application.Contents.Key(iNumber) & "<FONT COLOR=""#3333FF"">[Object]</FONT><BR>"
        Else
            If IsArray(Application.Contents(iNumber)) Then
                HTML = HTML & Application.contents.Key(iNumber) & "<FONT COLOR=""#3333FF"">[Array]</FONT><BR>"
            Else
                HTML = HTML & Application.contents.Key(iNumber) & ": <FONT COLOR=""#3333FF"">" & Application.Contents(iNumber) & "</FONT><BR>"
            End If
        End If
    Next

    HTML = HTML & "<hr>"

  
'Report the contents of the Server Variables collection
    HTML = HTML & "<b>Server Variables:</b><br>"

    For Each CollectionItem in request.servervariables
        If CollectionItem <> "ALL_HTTP" and CollectionItem <> "ALL_RAW" then
            HTML = HTML & CollectionItem & " : <FONT COLOR=""#3333FF"">" & request.servervariables(CollectionItem) & "</FONT><br>"
        End If
    Next

    HTML = HTML & "</body>"
    HTML = HTML & "</html>"

    'Send the error report using email. This currently uses ASPMail from serverobjects.com, but could be
    '
adapted to use another mail sending object (e.g. CDONTS) if required
    Set myMail = Server.CreateObject("SMTPsvg.Mailer")

    myMail.RemoteHost = MAIL_HOST
    myMail.FromName = MAIL_FROM_NAME
    myMail.FromAddress = MAIL_FROM_EMAIL

    myMail.AddRecipient MAIL_TO_NAME, MAIL_TO_EMAIL
    myMail.Subject = MAIL_SUBJECT
    myMail.BodyText = HTML
    myMail.ContentType = "text/html"

    If Not myMail.SendMail then

        SendErrorEmail = 0

    Else

        SendErrorEmail = 1

    End If

    Set myMail = Nothing

End Function
%>

Using CDONTS to send the error email

The script has been configured to use ASPMail from ServerObjects Inc.. It is of course possible to change the script so that it uses a different server component to send email. The code for using the CDONTS component that is usually installed on Windows NT Server is below:

    'Send the error report using email. This currently uses CDONTS, but could be
    'adapted to use another mail sending object if required

    Set myMail = CreateObject("CDONTS.NewMail")

    With myMail
        .From = MAIL_FROM_NAME & "<" & MAIL_FROM_EMAIL & ">"
        .To = MAIL_TO_NAME & "<" & MAIL_TO_EMAIL & ">"
        .Subject = MAIL_SUBJECT
        .Value("MIME-Version") = "1.1"
        .BodyFormat=0
        .MailFormat=0
        .Body=HTML
        .Send
    End With

    Set myMail = Nothing

Summary

The function described on this page can be used to send a comprehensive report to help with diagnosing and solving ASP errors on a site. If you have any suggestions for further enhancements, then send me an email!

Code samples

Further reading

Useful Development Tools

ASP Documentation Tool™
Automatically creates technical documentation for ASP 2.0 and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (5.2Mb ZIP file).

.NET Documentation Tool
Automatically creates technical documentation for .NET Framework applications written in C# or VB.NET (including ASP.NET). Documentation for SQL Server 7/2000/2005 databases and C#/VB.NET components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats. Additional support for ASP.NET web applications. A useful alternative to NDoc!
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (3Mb ZIP file).

SQL Documentation Tool
The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0, 2000 and 2005 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers, Table Relationships, Jobs and DTS Packages.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (10.3Mb ZIP file).

VB Documentation Tool
The VB Documentation Tool creates technical documentation for Microsoft Visual Basic 6.0 projects. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (1Mb ZIP file).

The Website Utility
The Website Utility examines websites for errors and areas that need to be optimised for search engines by using a built in web crawling engine. Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags. It also generates HTML for use in creating website site maps (table of contents pages - like this one), and is able to create both client-side JavaScript search engines and server-side ASP search engines and ASP.NET search engines for a website.
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (3Mb ZIP file).

Text Workbench
Text Workbench is a file search and replacement utility for text files and Microsoft Office documents. Make rapid file replacements on multiple files and folders full of files. Advanced replacement options include regular expressions support. It even works on remote file systems via FTP. A Regular Expression Laboratory allows advanced pattern matching and replacement expressions to be built and tested. This great utility will make your everyday development tasks much easier!
   Download Trial Version of Text Workbench Download Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation).

Indexing Service Companion
The Indexing Service Companion is a utility that extends the functionality of the Microsoft Windows Indexing Service so that it is able to index content from any remote website and also from ODBC compliant databases. As such it can be used as a low cost alternative to Sharepoint's Search Services.
   View Product Documentation View Product Documentation (119K ZIP file).
   Try Sample Search Facility Try Sample Search Facility.
   Download Trial Version Download Trial Version (1.7Mb ZIP file).

ASP Spell Check
ASPSpellCheck is the easy way to add spell checking capabilities to your ASP or ASP.NET websites, Intranets and web applications. The utility allows you to add spell checking capabilities to any HTML text field or rich content editing text box. It works with all common web browsers, and there are no components or databases to install on the server.
   Read a review of the ASP Spell Check server component Read ASPSpellCheck Review.
   View Examples of the ASPSpellCheck component for adding spell checking capabilities to ASP web applications View ASPSpellCheck Examples.
   Download Trial Version of ASPSpellCheck Download Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation).

Author details

Brett Burridge has worked as a web developer since 1997 and has developed web applications for a range of corporations, start up busiensses and educational establishments.

Brett is presently employed as an Internet developer and technical writer through his own company, Winnersh Triangle Web Solutions Limited. The company produces a number of innovative products, including a range of software documentation tools, which include the ASP Documentation Tool™, the .NET Documentation Tool for VB.NET and C#, and the SQL Server Documentation Tool. Other products include The Website Utility, which functions as a website error checker, search engine optimizer and ASP/ASP.NET search engine builder application.

As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk, ASPToday, the software documentation portal www.softwaredocumentation.info, and has contributed recipes to the ASP.NET Developer's Cookbook.    links

Outside web development, Brett is interested in travelling (here are my travel logs from New York, Hong Kong and Tokyo), digital photography (here's my photo gallery), tropical fishkeeping and collecting contemporary works of art by artists such as Doug Hyde.

Contact Brett by emailing

Indexing Service Companion - allows the Windows Indexing Service to index content from remote websites and ODBC databases!!!

Article history

"An ASP Error Report Emailer Function" published on ASPAlliance.com on 19 October 2001.

ASP Kitchen: Classic ASP Articles: An ASP Error Report Emailer

Documentation tools to automate the documentation of SQL Server databases and ASP, C#, VB.NET and VB 6.0 application source code

Download a Free ASP Documentation Tool Now!

Google

Search Engine Builder - Build a search engine for your website!

© page content copyright Brett Burridge 1998 - 2008.