Recently i found myself in need of the ability to email a status script to my team. Not only this, but it would also need to attach an file. This script would run when a service stopped, start the service with a batch file doing a simple “net start ‘service'”, querying the service with psservice then call a VBS file to email the results of the net start command. This gave us visability of crappy software services stopping and the attempted automated restart.
I came across this script on our corporate network, cant give the credit as dont know where its due, but its excellent for a free script. More info after the jump, its quite a large script.
dtmDate = Date
strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),2)
bFlag = CDOSYS_Send_Email(“<server>”,””,””,”<Recipient1@email.com;Recipient2@email.com>”,””,””,”<From address>”,”<Subject>””<Message Body>”,”<attachment>”)
On Error Resume Next
‘These weird constants do NOT cause any communications with schemas.microsoft.com. They are just naming conventions.
Const cdoSendUsingMethod = “http://schemas.microsoft.com/cdo/configuration/sendusing”
Const cdoSMTPServer = “http://schemas.microsoft.com/cdo/configuration/smtpserver”
Const cdoSMTPServerPort = “http://schemas.microsoft.com/cdo/configuration/smtpserverport”
Const cdoSMTPConnectionTimeout = “http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”
Const cdoSMTPAuthenticate = “http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”
Const cdoSendUserName = “http://schemas.microsoft.com/cdo/configuration/sendusername”
Const cdoSendPassword = “http://schemas.microsoft.com/cdo/configuration/sendpassword”
Const cdoSendUsingPickup = 1 ‘Use the local IIS-SMTP service for delivery.
Const cdoSendUsingPort = 2 ‘Use a remote SMTP server for delivery.
Set oMessage = WScript.CreateObject(“CDO.Message”)
Set oConfiguration = WScript.CreateObject(“CDO.Configuration”)
Set cFields = oConfiguration.Fields
cFields.Item(cdoSendUsingMethod) = cdoSendUsingPort ‘cdoSendUsingPort = Send to remote SMTP server (2), cdoSendUsingPickup = Send using local SMTP service (1).
cFields.Item(cdoSMTPServer) = sSmtpServer ‘IP address or DNS name of the remote SMTP server.
cFields.Item(cdoSMTPServerPort) = 25
cFields.Item(cdoSMTPConnectionTimeout) = 30 ‘Timeout in seconds for connection.
If LCase(sUsername) = “ntlm” Then
cFields.Item(cdoSMTPAuthenticate) = 2 ‘2 = NTLM authentication. NTLM uses the credentials of the WSH process itself.
ElseIf sUsername <> “” Then
cFields.Item(cdoSMTPAuthenticate) = 1 ‘1 = Basic authentication.
cFields.Item(cdoSendUserName) = sUsername ‘Username for Basic authentication, ignored for anonymous or NTLM authentication.
cFields.Item(cdoSendPassword) = sPassword ‘Password for Basic authentication, ignored for anonymous or NTLM authentication.
cFields.Item(cdoSMTPAuthenticate) = 0 ‘0 = Anonymous authentication.
cFields.Update ‘Save data so far.
Set oMessage.Configuration = oConfiguration
oMessage.To = sTo
oMessage.CC = sCC
oMessage.BCC = sBCC
oMessage.From = sFrom
oMessage.Subject = sSubject
oMessage.TextBody = sBody
If sAttach <> “” Then
aFiles = Split(sAttach,”;”)
For Each sFile In aFiles
If Err.Number = 0 Then
CDOSYS_Send_Email = True
CDOSYS_Send_Email = False
Set oMessage = Nothing
Set oFields = Nothing
Set oConfiguration = Nothing
‘END OF SCRIPT *********************************************************************
The above variables seem to translate to these:
Arguments: sSmtpServer = IP address or FQDN of out-going SMTP server.
‘ sUsername = (May be blank.) Username for Basic authentication; set blank for Anonymous; set ntlm for NTLM.
‘ sPassword = (May be blank.) Password for Basic authentication; ignored for Anonymous and NTLM.
‘ sTo = Recipient. Semicolon-delimited for multiple addresses.
‘ sCC = (May be blank.) CC recipient. Semicolon-delimited for multiple addresses.
‘ sBCC = (May be blank.) Blind CC recipient. Semicolon-delimited for multiple addresses.
‘ sFrom = Single e-mail address for sender.
‘ sSubject = (May be blank.) Subject line.
‘ sBody = (May be blank.) Body of message.
‘ sAttach = (May be blank.) Full path to local file to attach. Semicolon-delimited for multiple addresses.
if you are having difficulty with sending the email, ensure the server/desktop you run this script from can telnet to the exchange/smtp server on port 25. to test this use:
telnet <smtp address> 25
Below is a working excerpt of my script:
CDOSYS_Send_Email(“172.26.11.76″,””,””,”Andrew.Morgan@o2.com”,”SpaceChecks for DailyChecklist”,”E:NT InfrastructureDaily ChecksDublinSpaceResults” & strMonth & “-” & strDay & “-” & strYear & “.xls”)
Happy coding =)