VBS script sub routine to send e-mail

Needed something to send a e-mail from a VBS script when things were not going as planned, and thus came this sub. (If you plan to use this on a system with SMTP configured, then omit the configuration field lines)

Sub SendNotification (EmailFrom, EmailTo, EmailSubject, ErrorMessage, FileAttachment)
	Set Notify = CreateObject("CDO.Message")
 	With Notify
 		.From = EmailFrom
 		.To = EmailTo
 		.Subject = EmailSubject
 		.TextBody = ErrorMessage
 		.AddAttachment FileAttachment
 		.Configuration.Fields.Item _
 		.Configuration.Fields.Item _
 		.Configuration.Fields.Item _
 	End With
 	Set Notify = Nothing
End Sub

Just Change the smtp.domain.com to either the FQDN or IP of your mail server.

Here is an example of how to call the routine in your script:

  SendNotifcation _
"johndoe@domain.com", "janedoe@domain.com", "Error in Script", "Error In line 12", "c:\Logs.txt"

About Paul Cardelli, CISSP
Cyber Security Analyst, and computer guru

One Response to VBS script sub routine to send e-mail

  1. NovaSam says:

    By the way you can omit the configuration fields if you have the SMTP service installed on the system you plan to run the script on.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: