Vbscript to send mail

  • I am looking to code for a batch job to send out an email if the output text file from a job has a particular word. Trying to see if I can use vbscript for that. Has anybody done anything like that?

    Thanks,

    Vidhya

  • Hi vidya

    here is the complte script your looking for

    'Set the pattern you are looking for

    strPatternMatch = "wordtofind"

    Set objRegEx = CreateObject("VBScript.RegExp")

    objRegEx.Pattern = strPatternMatch

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Set Folder = objFSO.GetFolder(strFolderToPatternMatch)

    'For Each File In Folder.Files

    ' if UCASE(right(File.Name,13)) = "LETTERPCE.LOG" then

    'Set objFile = objFSO.OpenTextFile(strFolderToPatternMatch & "\" & File.Name, ForReading)

    Set objFile = objFSO.OpenTextFile("a.txt") ', ForReading)

    Do Until objFile.AtEndOfStream

    strSearchString = objFile.ReadLine

    Set colMatches = objRegEx.Execute(strSearchString)

    If colMatches.Count > 0 Then

    strFound = "1"

    'For Each strMatch in colMatches

    ' LogFile.WriteLine """" & strPatternMatch & """" & " found in " & strFolderToPatternMatch & "\" & File.Name

    'Next

    End If

    Loop

    objFile.Close

    Wscript.Echo strFound

    ' now send a mail if we found the word we are looking for

    If strFound = "1" Then

    Set objEmail = CreateObject("CDO.Message")

    objEmail.From = "from@test.com"

    objEmail.To = "to@test.com"

    objEmail.Subject = "Log"

    objEmail.Textbody = "Log line"

    objEmail.Configuration.Fields.Item _

    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    objEmail.Configuration.Fields.Item _

    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _

    "smtphostip"

    objEmail.Configuration.Fields.Item _

    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    objEmail.Configuration.Fields.Update

    objEmail.Send

    End IF

    ' End If

    'Next

    Shekhar.. :))


    Thanks ,

    Shekhar

  • Shekhar,

    Thanks for the script. I modified it at my end and am trying to run it and I only get a blank box pop up. It is not sending mail. Can you tell where I am wrong here?

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Set Folder = objFSO.GetFolder(strFolderToPatternMatch)

    'For Each File In Folder.Files

    ' if UCASE(right(File.Name,13)) = "test.log" then

    'Set objFile = objFSO.OpenTextFile(strFolderToPatternMatch & "\" & File.Name, ForReading)

    Set objFile = objFSO.OpenTextFile("C:\SQLjobs\oncall\log\test.log") ', ForReading)

    Do Until objFile.AtEndOfStream

    strSearchString = objFile.ReadLine

    Set colMatches = objRegEx.Execute(strSearchString)

    If colMatches.Count > 0 Then

    strFound = "1"

    'For Each strMatch in colMatches

    ' LogFile.WriteLine """" & strPatternMatch & """" & " found in " & strFolderToPatternMatch & "\" & File.Name

    'Next

    End If

    Loop

    objFile.Close

    Wscript.Echo strFound

    ' now send a mail if we found the word we are looking for

    If strFound = "1" Then

    Set objEmail = CreateObject("CDO.Message")

    objEmail.From = "vidhya.kumar@cree.com"

    objEmail.To = "vidhya.kumar@cree.com"

    objEmail.Subject = "SQL Server not responding"

    objEmail.Textbody = "Unable to connect to SQL Server"

    objEmail.Configuration.Fields.Item _

    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    objEmail.Configuration.Fields.Item _

    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _

    "smtp-outsrvr.cree.com"

    objEmail.Configuration.Fields.Item _

    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    objEmail.Configuration.Fields.Update

    objEmail.Send

    End IF

    ' End If

    'Next

  • Thanks! I figured it out. Its working great!!

    Vidhya.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply