Question

Locked

Send file in body of email not as attachment vbscript

By xroadtripx ·
Okay,

First of all I am very very new to scripting...I am running ntbackup via a scheduled task and have a vbscript running (that I found on the web) that sends me an email with the log file attached after every backup. I was hoping to have the text in the log file sent in the body of the email so I don't have to open it everyday. (I have been looking around and can't find the proper syntax). Here is the script I am using:

' Dim variables
Dim oFSO, oWshNetwork, oCDO, oFolder, oFiles, oFile
dim strFileName, strComputerName

' Create Objects
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNetwork = WScript.CreateObject("WScript.Network")


' Set constants
Const BACKUP_LOG_PATH = "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"
Const EMAIL_RECIPIENT = "my email@.com"

' Set variable defaults
strFileName = ""
strComputerName = oWshNetwork.ComputerName

' Start script
Set oFolder = oFSO.GetFolder(BACKUP_LOG_PATH)

Set oFiles = oFolder.Files

For Each oFile in oFiles
If oFSO.GetExtensionName(oFile.Name) = "log" Then
If DateValue(oFile.DateLastModified) = Date() Then
strFileName = oFile.Name
Exit For
End If
End If
Next

If strFileName <> "" Then

EmailLog(strFileName)

Else

EmailError

End If

Sub EmailLog(logFilePath)

Set oCDO = CreateObject("CDO.Message")

oCDO.Subject = "Backup Log - " & strComputerName
oCDO.From = strComputerName & "@domain.com"
oCDO.To = EMAIL_RECIPIENT

oCDO.TextBody = "Backup log attached for " & strComputerName

oCDO.AddAttachment BACKUP_LOG_PATH & "\" & logFilePath

oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="my mail server"

oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

oCDO.Configuration.Fields.Update

oCDO.Send

set oCDO = Nothing


End Sub

Sub EmailError

Set oCDO = CreateObject("CDO.Message")

oCDO.Subject = "Backup Log Missing - " & strComputerName
oCDO.From = strComputerName & "@domain.com"
oCDO.To = EMAIL_RECIPIENT

oCDO.TextBody = "No backup log found on " & strComputerName & " for today"

oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="my mailserver"

oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

oCDO.Configuration.Fields.Update

oCDO.Send

set oCDO = Nothing


End Sub

set oFSO = Nothing
Set oWshNetwork = Nothing

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

quick reply

by netwrk_admn In reply to Send file in body of emai ...

I have to run soon but I caught this. How about, if there is a log file, open the log file and read each line, posting it into the email body.

Loops until end of file reached. Quick and dirty.

Collapse -

thank you for your quick response

by xroadtripx In reply to quick reply

How would I do that?

Collapse -

Read each line, adding it to an existing string

by netwrk_admn In reply to thank you for your quick ...

Logically, you would want to set a string to <blank> and start reading the lines, one by one in strFileName. And you'll also want to keep the line breaks, so enter a vbCRLF in between each line read.
Forgot to add, you have to Open the file.

oFSO.OpenTextFile(strFileName)
text = ""

Do Until strFileName.AtEndOfStream
text = text + vbCRLF + strFileName.ReadLine
Loop

Then add it to your email body. OCDO.TextBody will be overwritten each time you declare it - it is NOT cumulative. So play around with what you want but only use the command once before sending email... like :

oCDO.TextBody = "Backup log attached for " & strComputerName & vbCRLF & vbCRLF & text

It appears to work fine on a quick test I did on an existing script.

Collapse -

I can't get it to work

by xroadtripx In reply to Read each line, adding it ...

I know what you wrote is a good script I just don't know where to insert it and I have no idea what is going wrong. I get a vb error when I run it, it stops on the .AtEndOfStream and says expected end of statement. I tried using a then and end. I should really read up on VB because I get the concepts understand what it wants but I think I am unsure how the script functions.

I appreciate all your help.

Collapse -

Got part of it to work

by xroadtripx In reply to Read each line, adding it ...

I am back :) Thank you for all your help. I was able get it to kind of work. The issue I am having is that is it just sending me the name of the log file backup10.log (for example ) instead of the data in backup10.log. Would you have any idea of why this is happening?

Collapse -

Try this

by mafergus In reply to Got part of it to work

This will read the entire file in one pass.

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = oFSO.OpenTextFile(strFileName)
text = ""

text = objTextFile.ReadAll
objTextFile.Close

Collapse -

No dice

by xroadtripx In reply to Try this

I still keep on getting the file name not the contents of the file

Collapse -

show me the mail portion of your code

by mafergus In reply to No dice

Just a hunch, but is that section using text or strfilename as a variable? also are you passing the text variable into the subroutine

Sub EmailLog(logFilePath,text)

Back to Networks Forum
8 total posts (Page 1 of 1)  

Related Forums