Logon Script to Send Email Notifications - Putting it all together
(Page 4 of 4 )
With all of the pieces in place, it’s time to put this all together. We need to start by setting the necessary variables for the SendMail function. These should be configured properly before this script is deployed.
' Please indicate where notifications should be sent
Const ADMIN_EMAIL = "recipient@mail.com"
' Please provide the following details for your SMTP server
Const SMTP_SERVER = "smtp.mail.com"
Const SMTP_PORT = 25 ' Do not change if you are unsure
' If your SMTP server requires authentication, please set
' USE_AUTHENTICATION to True and supply a username and password
Const USE_AUTHENTICATION = False
Const SMTP_USER = "username"
Const SMTP_PASS = "password"
' If your SMTP server uses Secure Password Authentication, please
' set the following value to True.
Const SMTP_SSL = False
This code should be at the beginning of your script. Set any of the necessary constants to ensure that your email will be sent properly. If you don’t have access to an SMTP server, there are free mail sending services available on the Internet.
Now it’s time to gather our last bit of details and put our email message together.
dteTime = Time
dteDate = Date
Now is a good time to grab the local date and time stamps. This can be helpful for tracking logons at a later date.
strMessage = "A user has logged onto <b>" & ComputerName & _
"</b> from <b>" & WAN_IP & _
"</b> with the following details:<br><br>" _
& "Logon Date: " & dteDate & "<br>" _
& "Logon Time: " & dteTime & "<br>" _
& "Account Name: " & AccountName & "<br>" _
& "LAN IP: " & LAN_IP & "<br>"
Here we’re simply assembling a text string containing a simple email message with all of the logon details that have been provided by our functions.
result = SendMail(strMessage)
Now we take our message body and pass it to the SendMail function. This one line does the dirty work.
That’s the concept in a nutshell. All that’s left is to set this as a logon script on each of your machines. In a networked environment, this can usually be done from the server.
You can download the full source code for this article below to see the complete script in its entirety. I’ve also added a little bonus that allows you to debug the script before putting it into general use.
Thanks for reading. Have some fun with this script. Until next time, keep coding!

| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |