Solved

VBScript to Attach a File to an Email

Posted on 2009-05-20
2
2,683 Views
Last Modified: 2013-11-30
Hi folks, I've got a vbscript I've put together that I plan to use to send out a log file once a day.  The trick I'm running into is that there's no way to make the name of the log file static so I can reference it directly in the script.  What I'd like to figure out then is how I can have the script just grab the first file it sees in the folder and attach it in the email.  Here's what I've got so far:
'Send Log File

Set objMessage = CreateObject("CDO.Message") 

objMessage.Subject = "Example CDO Message" 

objMessage.From = "email@server.com" 

objMessage.To = "email@server.com" 

objMessage.TextBody = "This is some sample message text."

objMessage.AddAttachment "C:\Snare\Logs\*.log"
 

'==This section provides the configuration information for the remote SMTP server.

'==Normally you will only change the server name or IP.

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") = "mysmtpserver"
 

'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

Open in new window

0
Comment
Question by:sstoyer
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
Comment Utility
This revision attaches the most recently-modified .log file in the specified directory.


strFolder = "C:\Snare\Logs"

strExt = "log"
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolder)
 

For Each objFile in objFolder.Files

    strFileExt = objFSO.GetExtensionName(objFile.Path)

    

    If objFile.DateLastModified > dtmRecent And LCase(strFileExt) = LCase(strExt) Then

        dtmRecent = objFile.DateLastModified

        strRecentFile = objFile.Path

    End If

Next
 

'Send Log File

Set objMessage = CreateObject("CDO.Message") 

objMessage.Subject = "Example CDO Message" 

objMessage.From = "email@server.com" 

objMessage.To = "email@server.com" 

objMessage.TextBody = "This is some sample message text."

objMessage.AddAttachment strRecentFile

 

'==This section provides the configuration information for the remote SMTP server.

'==Normally you will only change the server name or IP.

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") = "mysmtpserver"

 

'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

Open in new window

0
 

Author Closing Comment

by:sstoyer
Comment Utility
Worked like a charm, thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now