Send Email with previous day's error log detail

This section contains scripts that hMailServer has contributed with. hMailServer 4 is needed to use these.
Post Reply
User avatar
mattg
Moderator
Moderator
Posts: 20789
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Send Email with previous day's error log detail

Post by mattg » 2008-11-25 04:44

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: Select all

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
Last edited by mattg on 2008-12-03 02:18, edited 2 times in total.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

bigcrawdaddy
Normal user
Normal user
Posts: 53
Joined: 2008-02-19 14:41

Re: Send Email with previous day's error log detail

Post by bigcrawdaddy » 2008-12-02 14:43

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: Select all

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


User avatar
mattg
Moderator
Moderator
Posts: 20789
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Send Email with previous day's error log detail

Post by mattg » 2008-12-03 02:20

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.
https://www.hmailserver.com/documentation

Post Reply