Solved

VBScript to Attach a File to an Email

Posted on 2009-05-20
2
2,803 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
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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