Option Explicit
' Email variables:
Dim strServer, strTo, strFrom, strSubject, strBody
strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com"
strFrom = "john.doe@abc.com"
strSubject = "Subject Here"
strBody = "This is the body:" & vbCrLf
Dim dKeyWords
Set dKeyWords = CreateObject("Scripting.Dictionary")
dKeyWords.add "error", ""
dKeyWords.add "fault", ""
Const cInputFile = "C:\dev\EEPollFileForText\inputfile.txt"
Const forReading = 1
Dim oFSO, oFile, sContentsOfFile, dictionaryItem
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(cInputFile) Then
Set oFile = oFSO.OpenTextFile(cInputFile, forReading)
sContentsOfFile = oFile.ReadAll
sContentsOfFile = LCase(sContentsOfFile)
For Each dictionaryItem In dKeyWords
If InStr(1,sContentsOfFile,LCase(dictionaryItem),vbTextCompare) <> 0 Then
SendEmail strServer, strTo, strFrom, "Words found in text file", "Found the word " & dictionaryItem & " in " & cInputFile, ""
End If
Next
Else
SendEmail strServer, strTo, strFrom, "Text file not found", oInputFile & " was not found!"
End If
Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
Dim objMessage
Set objMessage = CreateObject("CDO.Message")
objMessage.To = strTo
objMessage.From = strFrom
objMessage.Subject = strSubject
objMessage.TextBody = strBody
If strAttachment <> "" Then objMessage.AddAttachment strAttachment
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
Set objMessage = Nothing
End Sub
You need to specify the path in the cInputFile line and you can add or change the keywords with the dKeyWords dictionary.
Having knocked this up, it has made me wonder about your specification. Is it enough to know that one of your keyword is somewhere is the file or do you want a nearby chuck on log inculded in the email? How should the script handle subsequent passes? Multiple instances of the same keyword?
Open in new window