본문 바로가기
dev/webDev

500 에러 페이지 내용 메일로 받기.

by Kelvin™ 2010. 11. 25.
자신이 개발한 홈페이지에서 404 에러는 무시해도 좋으나 500 에러는 프로그램 오류 이기 때문에 항시 모니터링 해야 한다.

IIS 셋팅에서 500 에러에 관련 내용을 첩부된 내용이 있는 곳을 지정하여 사용한다.




<%@ language="VBScript" %>
<%
  ' 에러 메시지 출력 페이지
  ' 2009년 5월 25일 작성

  Option Explicit

  Const lngMaxFormBytes = 200

  Dim objASPError, blnErrorWritten, strServername, strServerIP, strRemoteIP
  Dim strMethod, lngPos, datNow, strQueryString, strURL
  Dim errormain

  If Response.Buffer Then
    Response.Clear
    Response.Status = "500 Internal Server Error"
    Response.ContentType = "text/html"
    Response.Expires = 0
  End If

  If InStr(Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT")),"http://ws.daum.net/aboutWebSearch.html") > 0 Then
    Response.End
  End If

  If InStr(Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT")),"WebTrends") > 0 Then
    Response.End
  End If

  If InStr(Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT")),"InfoPath") > 0 Then
    Response.End
  End If

  If InStr(Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT")),"EmbeddedWB") > 0 Then
    Response.End
  End If

  If InStr(Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT")),"Googlebot") > 0 Then
    Response.End
  End If

  Set objASPError = Server.GetLastError
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>

<head>
<style>
a:link            {font:9pt/11pt 굴림; color:red}
a:visited        {font:9pt/11pt 굴림; color:#4e4e4e}
</style>
<meta HTTP-EQUIV="Content-Type" Content="text-html; charset=ks_c_5601-1987">
<title>HTTP 500 내부 서버 오류</title>
</head>
<script>

function Related(){
    userURL=document.location.href;

    //for the href, we need a valid URL to the domain. We search for the # symbol to find the begining
    //of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.

    BeginURL=userURL.indexOf("#",1) + 1;
    CurrentURL=userURL.substring(BeginURL,DocURL.length);

    //Build the query
    RelatedServiceURL="http://related.msn.com/related.asp?url=";

    //Perform simple check for Intranet URLs
    //this is where the http or https will be, as found by searching for :// but skip res:
    protocolIndex=userURL.indexOf("://",4);
    serverIndex=userURL.indexOf("/",protocolIndex + 3);
    urlresult=userURL.substring(0,serverIndex);
    if (protocolIndex - BeginURL > 7)
        urlresult=""

    //Check if Intranet URL - then open search bar

    if (urlresult.indexOf(".",0) < 1) userURL="Intranet URL";
    finalURL = RelatedServiceURL + encodeURIComponent(userURL);
    window.open(finalURL, "_search");

}

function Homepage(){

// in real bits, urls get returned to our script like this:
// res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm

    //For testing use
    //DocURL = "res://shdocvw.dll/http_404.htm#https://www.microsoft.com/bar.htm"
    DocURL=document.location.href;

    //this is where the http or https will be, as found by searching for :// but skipping the res://
    protocolIndex=DocURL.indexOf("://", 4);

    //this finds the ending slash for the domain server
    serverIndex=DocURL.indexOf("/", protocolIndex + 3);

    //for the href, we need a valid URL to the domain. We search for the # symbol to find the begining
    //of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.
    //urlresult=DocURL.substring(protocolIndex - 4,serverIndex);
    BeginURL=DocURL.indexOf("#",1) + 1;
    urlresult=DocURL.substring(BeginURL, serverIndex);
    if (protocolIndex - BeginURL > 7)
        urlresult=""

    //for display, we need to skip after http://, and go to the next slash
    displayresult=DocURL.substring(protocolIndex + 3, serverIndex);

    var aElement = document.createElement("A");
    aElement.innerText = displayresult;
    aElement.href = urlresult;

    homepageContainer.appendChild(aElement);
}

function doSearch()
{
    saOC.NavigateToDefaultSearch();
}

function initPage()
{
    document.body.insertAdjacentHTML("afterBegin","<object id=saOC CLASSID='clsid:B45FF030-4447-11D2-85DE-00C04FA35C89' HEIGHT=0 width=0></object>");
    Homepage();
}

</script>


<body bgcolor="white" onload="initPage()">

<table width="400" cellpadding="3" cellspacing="5">
  <tr>
    <td id="tableProps" valign="top" align="left"><img id="pagerrorImg" SRC="/errorpage/pagerror.gif"
    width="25" height="33"></td>
    <td id="tableProps2" align="left" valign="middle" width="360"><h1 id="errortype"
    style="COLOR: black; FONT: 13pt/15pt 굴림"><span id="errorText">페이지를 표시할 수 없습니다.</span></h1>
    </td>
  </tr>
  <tr>
    <td id="tablePropsWidth" width="400" colspan="2"><font
    style="COLOR: black; FONT: 9pt/11pt 굴림">연결하려는 페이지에 문제가 있어
    표시할 수 없습니다.</font></td>
  </tr>
  <tr>
    <td id="tablePropsWidth" width="400" colspan="2"><font id="LID1"
    style="COLOR: black; FONT: 9pt/11pt 굴림"><hr color="#C0C0C0" noshade>
    <p id="LID2">다음을 시도해 보십시오.</p><ul>
      <li id="instructionsText1"><span id="homepageContainer"></span>&nbsp; 홈 페이지를 열고 원하는 정보가 있는 링크를
        검색하십시오.  </li>
      <li id="instructionsText2"><a xhref="javascript:location.reload()" target="_self">
      <img border=0 src="/errorpage/refresh.gif" width="13" height="16"
        alt="refresh.gif (82바이트)" align="middle"></a> <a xhref="javascript:location.reload()" target="_self">새로 고침</a>
단추를 클릭하거나 나중에 다시 시도하십시오.<br>
      </li>
      <li ID="instructionsText3">인터넷에서 정보를 찾으려면 <a href="javascript:doSearch()"><img border=0 src="/errorpage/search.gif" width="16" height="16" alt="search.gif (114바이트)" align="center">검색</a>을 클릭하십시오. </li>
      <li id="instructionsText4"><a onclick="Related();event.returnValue=false" href="">관련 사이트 목록</a>을 참고할 수 있습니다.
</ul>
</p>

    <p><br>
    </p>
    <h2 id="ietext" style="font:9pt/11pt 굴림; color:black">HTTP 500 - 내부 서버 오류
    <br>
    Internet Explorer </h2>
    </font></td>
  </tr>
</table>
</body>
</html>
<%
errormain = "<META HTTP-EQUIV=""Content-Type"" Content=""text/html; charset=ks_c_5601-1987"">" & chr(13)
errormain = errormain & "<STYLE type=""text/css"">" & chr(13)
errormain = errormain & "  BODY { font: 9pt/12pt 굴림 }" & chr(13)
errormain = errormain & "  H1 { font: 13pt/15pt 굴림 }" & chr(13)
errormain = errormain & "  H2 { font: 9pt/12pt 굴림 }" & chr(13)
errormain = errormain & "  A:link { color: red }" & chr(13)
errormain = errormain & "  A:visited { color: maroon }" & chr(13)
errormain = errormain & "</STYLE><body style=""font-size:9pt;"">" & chr(13)
errormain = errormain & "<p>기술 정보(지원 인력용)</p>" & chr(13)
errormain = errormain & "<ul>" & chr(13)
errormain = errormain & "<li>오류 유형:<br>" & chr(13)

  Dim bakCodepage
  on error resume next
    bakCodepage = Session.Codepage
    Session.Codepage = 1252
  on error goto 0
errormain = errormain & Server.HTMLEncode(objASPError.Category)
  If objASPError.ASPCode > "" Then Response.Write Server.HTMLEncode(", " & objASPError.ASPCode)
    errormain = errormain & Server.HTMLEncode(" (0x" & Hex(objASPError.Number) & ")" ) & "<br>"
  If objASPError.ASPDescription > "" Then
      errormain = errormain & Server.HTMLEncode(objASPError.ASPDescription) & "<br>"
  elseIf (objASPError.Description > "") Then
      errormain = errormain & Server.HTMLEncode(objASPError.Description) & "<br>"
  end if
  blnErrorWritten = False
  ' Only show the Source if it is available and the request is from the same machine as IIS
  If objASPError.Source > "" Then
    strServername = LCase(Request.ServerVariables("SERVER_NAME"))
    strServerIP = Request.ServerVariables("LOCAL_ADDR")
    strRemoteIP =  Request.ServerVariables("REMOTE_ADDR")
    If (strServerIP = strRemoteIP) And objASPError.File <> "?" Then
      errormain = errormain & Server.HTMLEncode(objASPError.File)
      If objASPError.Line > 0 Then errormain = errormain & ", line " & objASPError.Line
      If objASPError.Column > 0 Then errormain = errormain & ", column " & objASPError.Column
        errormain = errormain & "<br>"
        errormain = errormain & "<font style=""COLOR:000000; FONT: 8pt/11pt courier new""><b>"
        errormain = errormain & Server.HTMLEncode(objASPError.Source) & "<br>"
      If objASPError.Column > 0 Then errormain = errormain & String((objASPError.Column - 1), "-") & "^<br>"
      errormain = errormain & "</b></font>"
      blnErrorWritten = True
    End If
  End If
  If Not blnErrorWritten And objASPError.File <> "?" Then
    errormain = errormain & "<b>" & Server.HTMLEncode(  objASPError.File)
    If objASPError.Line > 0 Then errormain = errormain &  Server.HTMLEncode(", line " & objASPError.Line)
    If objASPError.Column > 0 Then errormain = errormain & ", column " & objASPError.Column
    errormain = errormain & "</b><br>"
  End If

errormain = errormain & "</li>" & chr(13)
errormain = errormain & "<li>브라우저 종류:<br> " & Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT")) & "<br><br></li>" & chr(13)
errormain = errormain & "<li>페이지:<br>" & chr(13)

  strMethod = Request.ServerVariables("REQUEST_METHOD")
  errormain = errormain & strMethod & " "
  If strMethod = "POST" Then
    errormain = errormain & Request.TotalBytes & " bytes to "
  End If
  errormain = errormain & Request.ServerVariables("SCRIPT_NAME")
  errormain = errormain & "</li>"
  If strMethod = "POST" Then
    errormain = errormain & "<p><li>POST Data:<br>"
    ' On Error in case Request.BinaryRead was executed in the page that triggered the error.
    On Error Resume Next
    If Request.TotalBytes > lngMaxFormBytes Then
      errormain = errormain & Server.HTMLEncode(Left(Request.Form, lngMaxFormBytes)) & " . . ."
    Else
      errormain = errormain & Server.HTMLEncode(Request.Form)
    End If
    On Error Goto 0
    errormain = errormain & "</li>"
  End If

errormain = errormain & "<br><br></li>"
errormain = errormain & "<li>시간:<br>"

  datNow = Now()
  errormain = errormain & Server.HTMLEncode(FormatDateTime(datNow, 1) & ", " & FormatDateTime(datNow, 3))
  on error resume next
    Session.Codepage = bakCodepage
  on error goto 0
errormain = errormain & "<br><br></li>"
errormain = errormain & "<li>사용자 정보:<br>"

  errormain = errormain & "IP address : " & Request.ServerVariables("REMOTE_ADDR") & "<br>"
  errormain = errormain & "URL : " & LCase(Request.ServerVariables("SERVER_NAME")) & Lcase(Request.ServerVariables("URL")) & "<br>"

errormain = errormain & "<br><li>Session Contents : <br>"

Dim key

For Each key in Session.Contents
  errormain = errormain & key & " : " & Session(key) & "<br>"
Next

errormain = errormain & "<br><li>Cookies Value<br>"

For Each key in Request.Cookies
  errormain = errormain & key & " : " & Request.cookies(key) & "<br>"
Next

errormain = errormain & "<br><li>Form Value<br>"

For Each key in Request.Form
  errormain = errormain & key & " : " & Request.Form(key) & "<br>"
Next

errormain = errormain & "<br><li>QueryString Value<br>"

For Each key in Request.QueryString
  errormain = errormain & key & " : " & Request.QueryString(key) & "<br>"
Next

  on error goto 0

errormain = errormain & "<br><br></li>"
errormain = errormain & "</ul>"

If instr(Request.ServerVariables("server_software"),"6.0") > 0 then

    Dim sch, cdoConfig, cdoMessage

    sch = "http://schemas.microsoft.com/cdo/configuration/"

    Set cdoConfig = CreateObject("CDO.Configuration")

    With cdoConfig.Fields
     .Item(sch & "sendusing") = 1               'cdoSendUsingPort 1=내부, 2=외부
     .Item(sch & "smtpserverport") = 25     'SMTP Port
     .Item(sch & "smtpserver") = "localhost"     'Mail Server Address or Domain or "localhost"
     .update
    End With

    Set cdoMessage = CreateObject("CDO.Message")

    With cdoMessage
      Set .Configuration = cdoConfig
     .From = ""                       '보내는이 메일주소
     .To = ""                        '받는이 메일주소
     .Subject = LCase(Request.ServerVariables("SERVER_NAME")) & Lcase(Request.ServerVariables("URL")) & " 오류입니다."                   '메일제목
     .HtmlBody = errormain  '메일형식지정 : HtmlBody or TextBody
      .BodyPart.Charset="ks_c_5601-1987"
       .HTMLBodyPart.Charset="ks_c_5601-1987"
     .Send
    End With


    Set cdoMessage = Nothing
    Set cdoConfig = Nothing


Else   Dim objMail
  Set objMail = Server.CreateObject("CDONTS.NewMail")
  objMail.From        = ""
  objMail.To            = ""
  objMail.Subject        = LCase(Request.ServerVariables("SERVER_NAME")) & Lcase(Request.ServerVariables("URL")) & " 오류입니다."
  objMail.Body        = errormain
  objMail.BodyFormat    = 0    ' HTML일떄 0, 일반 Text일때 1 으로 설정한다.
  objMail.MailFormat    = 0     ' HTML일떄 0, 일반 Text일때 1 으로 설정한다.
  objMail.send               '이제 메일을 보낸다.
  SET objMail = NOTHING
End If

%>



'dev > webDev' 카테고리의 다른 글

크로스 브라우징 Image Opacity 예제  (0) 2011.02.14
짧은 URL 사용 개념 정리.  (0) 2011.02.07
티스토리 API 참고 사이트  (0) 2010.11.03
ms-sql paging  (0) 2010.10.29
iframe 페이지 자동 늘리기 소스  (0) 2010.10.29