Solved

VBScript to Attach a File to an Email

Posted on 2009-05-20
2
2,868 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Attention Please 10 54
Everything left of comma in excel 4 49
Excel 2007 Macro to Change Column Formatting 3 43
Can I pass my http referer through my VBScript? 1 21
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
When you’re making plans to join the modern business race, you should analyze various details that may affect your results. Nowadays, millions of businesses are trying to grow into established and appreciated professional enterprises.
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…

763 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