Emailing via VBS Script - Looking to add a attachment

Nick Collins
Nick Collins used Ask the Experts™
on
Good Afternoon,

I have this VBS script for sending emails but I want to add a section to the script where it looks for a particular file and adds it as attachment.

' Constants
Const cSmtpUser = "Username"                                ' *** MAKE CHANGES HERE ***
Const cSmtpPassword = "Password"                            ' *** MAKE CHANGES HERE ***
Const cSmtpServer = "smtp.test.net"                          ' *** MAKE CHANGES HERE ***
Const cSmtpPort = 25                                       ' *** MAKE CHANGES HERE *** (25, 465, 587 common)
Const cFromEmail = "admin@test.uk"                      ' *** MAKE CHANGES HERE ***
Const cToEmail = "nick@test.uk"                        ' *** MAKE CHANGES HERE ***
Const cSubject = "Daily Worksheet - Operatives Missing"                     ' *** MAKE CHANGES HERE ***

' CDO Constants needed to send email
Const cCdoSendUsingPickup = 1   'Send message using the local SMTP service pickup directory.
Const cCdoSendUsingPort = 2     'Send the message using the network (SMTP over the network).
Const cCdoAnonymous = 0         'Do not authenticate
Const cCdoBasic = 1             'basic (clear-text) authentication
Const cCdoNTLM = 2              'NTLM
Const cCdoSendUsingMethod        = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cCdoSMTPServer             = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cCdoSMTPServerPort         = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cCdoSMTPConnectionTimeout  = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cCdoSMTPAuthenticate       = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cCdoSendUserName           = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cCdoSendPassword           = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const cCdoSmtpUseSsl             = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateTrue = -1
Const TristateFalse = 0
Const TristateUseDefault = -2


' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get a handle to the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")

' Set config fields we care about
With objConfig.Fields
    .Item(cCdoSendUsingMethod)       = cCdoSendUsingPort
    .Item(cCdoSMTPServer)            = cSmtpServer
    .Item(cCdoSMTPServerPort)        = cSmtpPort
    .Item(cCdoSMTPConnectionTimeout) = 60
    .Item(cCdoSMTPAuthenticate)      = cCdoBasic
    .Item(cCdoSendUserName)          = cSmtpUser
    .Item(cCdoSendPassword)          = cSmtpPassword
    .Item(cCdoSmtpUseSsl)            = True
    .Update
End With

' Create a new message
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig

' Populate message fields and send it
With objMessage
    .To       = cToEmail
    .From     = cFromEmail
    .Subject  = cSubject
    .TextBody = "*** Daily Worksheet Completed - Missing Operatives are detailed in the attached file ***" & vbNewLine & vbNewLine
    .Send
End With

Open in new window


I will look forward to your comments
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PeeterBIT Support Tech
Commented:
Does this example help ?
How Can I Attach a File to an Email Sent Using CDO?

looks like you just need one line ? (about half way down that page ..)
......
Now, what about adding an attachment to this email? All it takes is one additional line of code, much like this line which attaches the files C:\Scripts\Output.txt to the email:

objEmail.AddAttachment "C:\Scripts\Output.txt"
........

So, maybe add a new line between your 62 and 63 :
With objMessage
    .To       = cToEmail
    .From     = cFromEmail
    .Subject  = cSubject
    .TextBody = "*** Daily Worksheet Completed - Missing Operatives are detailed in the attached file ***" & vbNewLine & vbNewLine
    .AddAttachment "C:\Scripts\Output.txt"
    .Send
End With

Open in new window

IT / Software Engineering Consultant
Top Expert 2016
Commented:
Right before the .Send and .AddAttachment with the full path to the file to attach.  I'd recommend checking that the file exists beforehand so that no error occurs when adding it here.

I've posted that code as solution to a few questions here, if you search for "*** MAKE CHANGES HERE ***" you can probably find some with attachment examples.  As in:



»bp
PeeterBIT Support Tech

Commented:
@Nick -- thanks for the acknowledgements!  I can now see that @Bill already has some great solutions completed for previous similar questions and I very happy to just observe this thread now for a while ( and learn a lot more ! ) ... Best Wishes @Nick, @Bill

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial