Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBScript to Attach a File to an Email

Posted on 2009-05-20
2
Medium Priority
?
3,119 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 2000 total points
ID: 24434617
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
ID: 31583611
Worked like a charm, thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Screencast - Getting to Know the Pipeline

971 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