- Category: Scripts and Code
- Published on Tuesday, 30 April 2013 00:40
- Written by Christian Dunn
If you have servers to monitor and require automated email notifications then you need a VBScript that can be scheduled and run automatically. This script allows you to take any HTML file and send it as the content of an email and include another file as an attachment. I have used this in conjunction with scripts that generate server storage space using WMI so I can monitor disk usage. This email can constitute a daily or weekly report simplifying your management and monitoring. This script uses CDO to send mail and can be configured with or without authentication.
To trigger this script from a scheduled task it is recommended to use a command./batch file which is included with 32 bit and 64 bit example. Simply comment out the unrequired section depending on your environment. This script is designed to be run from your C drive but can be easily modified. Remember to configure your email settings before use. I have highlighted the sections you need to modify.
Dim HtmlBody 'To store html body
Dim EmailSubject 'To store email Subject
Dim EmailFrom 'To store email From
Dim EmailTo 'To store email To
Dim EmailBcc 'To store email BCC
Dim EmailCc 'To store email CC
Dim EmailSMTPServer 'To store SMTP Server name or address
Dim fso 'To declare File System Object for file access
Dim filename 'To declare filename for email content
Dim myattachment 'To declare filename for attachment
'Initialization of Email Configuration
EmailSMTPServer = "yourmailserver.com"
EmailSubject = "My Email"
EmailBcc = ""
EmailCc = ""
'declare file to use as HTML email content
filename = "c:\myemail.html"
'declare file to use as attachment
myattachment = "c:\myemail.html"
'Creating File System Object and loading HTML file into email content
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjOutFile = fso.OpenTextFile(filename,1)
HtmlBody = objOutFile.ReadAll
'Send the HTML email
'Specify Email sending properties
Dim SMTPServer, SMTPPort, SMTPAuthenticate, SMTPUserName, SMTPPassword, SMTPTimeout, SMTPProxyServer, SMTPProxyBypass
SMTPServer = EmailSMTPServer
SMTPPort = "25"
SMTPAuthenticate = "0"
SMTPUserName = ""
SMTPPassword = ""
SMTPTimeout = "5000"
SMTPProxyServer = ""
SMTPProxyBypass = ""
Set GetCDOFromProperties = ConfigureCDO(SMTPServer, SMTPPort, SMTPAuthenticate, SMTPUserName, SMTPPassword, SMTPTimeout, SMTPProxyServer, SMTPProxyBypass)
'Configure CDO Email
Function ConfigureCDO(SMTPServer, SMTPPort, SMTPAuthenticate, SMTPUserName, SMTPPassWord, SMTPTimeout, SMTPProxyServer, SMTPProxyBypass)
' Setup SMTP Options
Set Conf = CreateObject("CDO.Configuration")
'Specifies the method used to send messages. 1 - Pickup; 2 - Port. If a local SMTP service is available, this field defaults to 1
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' The pickup directory for the local SMTP service. This value is set automatically when the SMTP service is installed.
'Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\mailroot\pickup"
' The name (DNS) or IP address of the machine hosting the SMTP service through which messages are to be sent.
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strnullcheck(SMTPPort)
' The port on which the SMTP service specified by the smtpserver field is listening for connections. The default and well-known port for an SMTP service is 25.
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strnullcheck(SMTPServer)
' SMTP is using SSL; Default False
'Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
' 0 - Do not auth; 1 - Basic (clear text auth); 2 - Use NTLM authentication (Secure Password Authentication in MicrosoftÂ® OutlookÂ® Express) The current process security context is used to authenticate with the service.
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = strnullcheck(SMTPAuthenticate)
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strnullcheck(SMTPUserName)
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strnullcheck(SMTPPassWord)
' The user's email address used when connecting to the SMTP service.
' Indicates the number of seconds to wait for a valid socket to be established with the SMTP service before timing out. Default is 30
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = strnullcheck(SMTPTimeout)
' The proxy server to use when accessing HTTP resources. Both the name (or IP address) of the proxy machine and the port number must be specified using the format "servername:port."
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/urlproxyserver") = strnullcheck(SMTPProxyServer) '"server:80"
' Used to specify that for local addresses, the proxy (set with urlproxyserver) should be bypassed. The value of the string, if set, should only be "<local>". This is the same value used with Internet Explorer when setting a proxy with bypass for local addresses.
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/urlproxybypass") = strnullcheck(SMTPProxyBypass) '"<local>"
Set ConfigureCDO = Conf
'Used to check for blank values in CDO Configuration
If Isnull(varString) Then
strnullcheck = ""
strnullcheck = varString
'Send the email (including an attachment)
Set objMessage = CreateObject("CDO.Message")
objMessage.Configuration = GetCDOFromProperties()
objMessage.Subject = EmailSubject
objMessage.From = EmailFrom
objMessage.To = EmailTo
objMessage.Bcc = EmailBcc
objMessage.Cc = EmailCc
'Add Email Attachment
'The line below shows how to send using HTML included directly in your script
objMessage.HTMLBody = HtmlBody
Set objMessage = nothing
HtmlBody = ""
REM 32 bit start script
REM "C:\Windows\System32\cscript.exe" "c:\sendemail.vbs"
REM 64 bit start script