Solved

VBScript to Attach a File to an Email

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
smtp question 3 64
'Include' Syntex in domain records 6 48
How can I modify this Excel worksheet with a script? 15 73
Macro Excel - Multiple If conditions 2 63
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

22 Experts available now in Live!

Get 1:1 Help Now