Writing subject in header to log

Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.
Post Reply
CraigT
New user
New user
Posts: 8
Joined: 2010-08-12 10:06
Location: Adelaide, Australia

Writing subject in header to log

Post by CraigT » 2020-04-09 04:40

Hi all..
Does anyone know of a script that will write the subject header into the server log when an email is delivered to a mailbox. Unfortunately, certain persons have been deleting critical emails from their mailbox and then claiming they never received them. This would make delivery stand out. I assume that it may be simple but my knowledge of the inner workings and VB is limited.

Virinum
Normal user
Normal user
Posts: 93
Joined: 2018-11-23 14:42
Location: Germany

Re: Writing subject in header to log

Post by Virinum » 2020-04-09 07:43

Add this to your EventHandlers.vbs

Code: Select all

  Sub OnDeliverMessage(oMessage)
    EventLog.Write oMessage.Subject
  End Sub

CraigT
New user
New user
Posts: 8
Joined: 2010-08-12 10:06
Location: Adelaide, Australia

Re: Writing subject in header to log

Post by CraigT » 2020-04-10 05:44

Thanks

CraigT
New user
New user
Posts: 8
Joined: 2010-08-12 10:06
Location: Adelaide, Australia

Re: Writing subject in header to log

Post by CraigT » 2020-04-15 10:45

Having implemented the code I found that it created a separate event log with just the subject line showing. After a bit of research I inserted a line above to display the From Address

Code: Select all

 Sub OnDeliverMessage(oMessage)
    EventLog.Write oMessage.FromAddress
    EventLog.Write oMessage.Subject
  End Sub
which actually worked!
The separate log is good from the admin point of view as it removes the server log chatter. I have been trying to find the To Address to add to the log, so it was From: Subject:To and then make it a daily log the same as the hMailserver log. I have a PowerShell script to zip up the files after a month.
Any help appreciated.

Virinum
Normal user
Normal user
Posts: 93
Joined: 2018-11-23 14:42
Location: Germany

Re: Writing subject in header to log

Post by Virinum » 2020-04-15 11:39

https://www.hmailserver.com/documentati ... ct_message
Recipients is a list. You have to loop over all recipients. Something like this (not tested):

Code: Select all

Sub OnDeliverMessage(oMessage)
  Dim i, logMessage
  logMessage = oMessage.FromAddress & ": " & oMessage.Subject & ": "
  For i = 0 To oMessage.Recipients.Count-1
    If (i = 0) Then
      logMessage = logMessage & oMessage.Recipients(i).OriginalAddress
    Else
      logMessage = logMessage & ", " & oMessage.Recipients(i).OriginalAddress
    End If
  Next
  EventLog.Write logMessage
End Sub
And for the daily log I've used this PowerShell script some time ago (executed every day at 0:00):

Code: Select all

$yesterday = (get-date).AddDays(-1).ToString("yyyy-MM-dd")
$path = "C:\Program Files (x86)\hMailServer\Logs"
$originalfile = "$path\hmailserver_events.log"
$newfile = "$path\hmailserver_events_$yesterday.log"
if ((Test-Path $originalfile) -And -Not (Test-Path $newfile)) {
    Rename-Item -Path $originalfile -NewName $newfile
}

CraigT
New user
New user
Posts: 8
Joined: 2010-08-12 10:06
Location: Adelaide, Australia

Re: Writing subject in header to log

Post by CraigT » 2020-04-15 11:59

Once again...thanks. Gives me something to play with, and also learn.

CraigT
New user
New user
Posts: 8
Joined: 2010-08-12 10:06
Location: Adelaide, Australia

Re: Writing subject in header to log

Post by CraigT » 2020-04-16 11:27

The code works well. The rotate log code can be used for other log files with a little modification. Once again thanks.

User avatar
SorenR
Senior user
Senior user
Posts: 3624
Joined: 2006-08-21 15:38
Location: Denmark

Re: Writing subject in header to log

Post by SorenR » 2020-04-16 11:49

Often simple is good... I was thinking of offering my own approach but the 200+ lines of code can wait until your VBScript improves ;-)
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

CraigT
New user
New user
Posts: 8
Joined: 2010-08-12 10:06
Location: Adelaide, Australia

Re: Writing subject in header to log

Post by CraigT » 2020-04-17 10:13

That's OK. I need to look at it. My background goes back to COBOL, Fortran, GWBasic and lately Bascom for micros. Once I get an understanding, it will make it a lot easier. :D

Post Reply