Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Send Email with previous day's error log detail
PostPosted: 2008-11-25 04:44 
Moderator
User avatar

Joined: 2007-06-14 05:12
Posts: 9562
Location: 'The Outback' Australia
G'day,

I have written this short vbs script to send an e-mail when there are errors the previous day, with details of those errors.

Save the script to any directory, and then create a scheduled task to run it once per day. I run mine (arbitrarily) at 7.00 am.
I haven't tested the first of each new month part of the script as yet, but I will update as appropriate if needed.

There are some 'change this to suit' throughout the script, make sure that you change them.

Save the script with a .vbs extension, something like eMailErrors.vbs

All care taken, no responsibility (but in saying that, this doesn't actually change anything on your system, just sends an e-mail)

Code:
Option Explicit

const g_sBNFrom = "Mail Server"     ' change this to suit
const g_sBNFromAddress = "server@domain.com"   ' change this to suit
const g_sAdminPassword = "secret"   ' change this to suit

dim MessageRecipientName, MessageRecipientAddress, MessageSubject
dim oApp, s, oMessage
Dim FSO, FileIn, Filebase, OBJfile
dim syear, smonth, sday, imonth, iday, iyear

Filebase = "C:\Program Files\hMailServer\Logs\ERROR_hmailserver_"

MessageRecipientName = "Matt"   'change this to suit
MessageRecipientAddress = "matt@domain.com"   'change this to suit
MessageSubject = "Hmailserver Errors"   'change this to suit

Set FSO = CreateObject("Scripting.FileSystemObject")
sYear = year(now)
iMonth = Month(now)
iDay = day(now)-1

If iDay = 0 Then 'If today is the first day of the month
  iMonth = iMonth - 1
End If

If iMonth = 4 OR iMonth = 6 OR iMonth = 9 Or iMonth = 11 Then
  iDay = 30
End If 'Set last day of last month
If iMonth = 1 OR iMonth = 3 OR iMonth = 5 OR iMonth = 7 OR iMonth = 8 OR iMonth = 10 Then
  iDay = 31
End If 'Set last day of last month
If iMonth = 2 Then
  If iYear/4 = int(iYear/4) and iYear/100 <> int(iYear/100) Then
    iDay = 29
  Else
    iDay = 28
  End If
End If 'Set last day of last month if last month is Feb and checks for Leap Year

If iMonth = 0 Then
  iYear = iYear - 1
  iMonth = 12
  iDay = 31
End If 'Set last Month and Day is today is Jan 1

If iDay < 10 Then
   sDay = "0" + cstr(iDay)
Else
   sDay = cstr(iDay)
End If 'Make all days two digits

If iMonth < 10 Then
   sMonth = "0" + cstr(iMonth)
Else
   sMonth = cstr(iMonth)
End If 'Make all Months two digits

sYear = cstr(iYear) 'Set Year

filein = filebase + sYear + "-" + sMonth + "-" + sDay + ".log"
If fso.FileExists(Filein) Then
   set OBJfile = FSO.opentextfile(filein,1,0)
   While Not OBJfile.atendofstream
      s = s & OBJfile.readline
   Wend
   OBJfile.close
   set Objfile = nothing

   Set oApp = CreateObject("hMailServer.Application")
   Call oApp.Authenticate("Administrator", g_sAdminPassword)
   Set oMessage = CreateObject("hMailServer.Message")

   oMessage.From = g_sBNFrom & " <" & g_sBNFromAddress &  ">"
   oMessage.FromAddress = g_sBNFromAddress
   oMessage.Subject = MessageSubject
   oMessage.AddRecipient MessageRecipientName, MessageRecipientAddress
   oMessage.Body = "The Following Errors have occured." & vbNewLine & vbNewLine & s
   oMessage.Save

End If

_________________
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
Documentation


Last edited by mattg on 2008-12-03 02:18, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Send Email with previous day's error log detail
PostPosted: 2008-12-02 14:43 
Normal user

Joined: 2008-02-19 14:41
Posts: 53
The Date didn't work for me went we moved to December so I made and tested the following changes to the code to set the date. Changed server date and tested end of each month, end of the year, and leap year.
Code:
If iDay = 0 Then 'If today is the first day of the month
  iMonth = iMonth - 1
End If

If iMonth = 4 OR iMonth = 6 OR iMonth = 9 Or iMonth = 11 Then
  iDay = 30
End If 'Set last day of last month
If iMonth = 1 OR iMonth = 3 OR iMonth = 5 OR iMonth = 7 OR iMonth = 8 OR iMonth = 10 Then
  iDay = 31
End If 'Set last day of last month
If iMonth = 2 Then
  If iYear/4 = int(iYear/4) and iYear/100 <> int(iYear/100) Then
    iDay = 29
  Else
    iDay = 28
  End If
End If 'Set last day of last month if last month is Feb and checks for Leap Year

If iMonth = 0 Then
  iYear = iYear - 1
  iMonth = 12
  iDay = 31
End If 'Set last Month and Day is today is Jan 1

If iDay < 10 Then
   sDay = "0" + cstr(iDay)
Else
   sDay = cstr(iDay)
End If 'Make all days two digits

If iMonth < 10 Then
   sMonth = "0" + cstr(iMonth)
Else
   sMonth = cstr(iMonth)
End If 'Make all Months two digits

sYear = cstr(iYear) 'Set Year



Top
 Profile  
 
 Post subject: Re: Send Email with previous day's error log detail
PostPosted: 2008-12-03 02:20 
Moderator
User avatar

Joined: 2007-06-14 05:12
Posts: 9562
Location: 'The Outback' Australia
Update to include your changes :mrgreen:

Also declared iyear, and changed the 'filein=' line just below where your changes end.

Again, I haven't tested, but looks good to me.
BTW my script here worked into December, perhaps I have it slightly different...

_________________
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
Documentation


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest



Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group