Wednesday, October 6, 2010

VBScript: Sending an e-mail via a script.

Over the years I've occasionally found it necessary to employ scripts in a fashion that makes monitoring their progress difficult or nearly impossible, given that I suffer from the human fallibility known as "needing to sleep". So to counteract this weakness and sustain the illusion that I'm some kind of omniscient IT-Deity I long ago looked into developing a way to have scripts communicate with me using that most ubiquitous of channels; e-mail.

No great trick, I know. There are tons of examples out there that will show you how to send an e-mail via script and this one isn't too dissimilar to the rest. The difference here would be in the way that I typically employ this trick, which is not something I've personally seen posted anywhere else; as part of error handling.

Granted I would suggest using this sparingly lest you be inundated with  little pops and pings from your scripts, but including something like this in those mission-critical scripts that have to run at all hours of the day or night can help you rest a little easier; or not, depending on the results. As a bonus the same function can be used to send an e-mail at any time throughout the script, so "start and stop" notifications can be configured just as easily if need be.

This particular sub is configured to work with Gmail as the sending account, primarily because it's free and easily accessible. If you're not able to use a web-accessible service you can pretty easily modify this to run off of your company's exchange system (or what have you).

The example below is straight forward; I purposely generate an error by making an illegal assignment to kick off the error checking routine and send an e-mail notification. I also stop the script from executing further, to keep the damage to a minimum (hopefully).


On Error Resume Next
Set Now = "This Value" 'Throw an error to be caught

if Err.Number <> 0 Then
msgSubject = Err.Description & " : " & Err.Number
msgText = Err.Description & vbCrlf
msgText = msgText & Err.Number & vbCrlf
msgText = msgText & Err.Source & vbCrlf
msgText = msgText & "If you get this e-mail, we're good to go!"
Call sendError("colonelhammer@yahoo.com", msgSubject, msgText)
Wscript.Quit
End if
msgbox "If you see this, I'm in trouble"



sub sendError(toField, subjectLine, msgBody)
Set objMsg = CreateObject("CDO.Message")

'Build your e-mail.

objMsg.From = "YourAddress@gmail.com" '<----Generally needs to match the address you're sending from!
objMsg.To = toField
objMsg.Subject = subjectLine
objMsg.TextBody = msgBody

'------------Back-end Configuration information for the remote SMTP server----------

objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'SMTP Server, name or by IP address.
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

'Type of authentication
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

'Your UserID on the SMTP server
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourAddress@gmail.com@gmail.com"

'Your password on the SMTP server
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YourPaswordHere"

'Server port (typically 25)
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'Use SSL for the connection (False or True)
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

'Connection Timeout in seconds
objMsg.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMsg.Configuration.Fields.Update

'------------Back-end Configuration information for the remote SMTP server----------

objMsg.Send

End Sub




Enjoy,

No comments: