You've Got Mail
The tool I use to send mail attachments is the simple and flexible Blat utility. The version of Blat that's current as of this writing is 1.9.4, but this code should also work fine with earlier versions. However, you need to be aware of a couple of Blat idiosyncrasies. First, if you try to access the online Help file by typing
blat /?
or
blat /help
in the command-shell window, you'll get an error message. So, to see the verbose option list, just type
blat.exe
in the command-shell window. Blat has an intimidating number of options. However, HTTPDownLoader.bat uses only some of them. If you were to type the Blat command that the script uses in the command-shell window, it would look like
blat D:\Fblat.txt
-attach D:\Stinger.exe
-s "New Stinger Version
Attached"
-server mail.yourcompany.com
-f dick@yourcompany.com
-t fred@yourcompany.com
This command's argument specifies the path to the text file that contains the body of the email message. The attach switch specifies the file you want to attach, the s switch specifies the subject line, the server switch specifies the SMTP mail server's address, the f switch specifies the email address of the person sending the email message, and the t switch specifies the email address of the intended recipient. Callout A in Listing 2 shows how this Blat command looks when you use variables rather than hard-coding the argument and switch values. The code at callout A sends an email notification about the utility's new version and attaches that new version. The Blat command above callout A sends only the email notification.
The second idiosyncrasy, which is obvious in the preceding command, is the necessity for a text file to contain the text of the message. Even if your message is extremely short, you can't enter it as an argument; you must place it in a text file that Blat uses as the message source.
Prelaunch Considerations
To benefit from HTTPDownLoader.bat, you need to heed a few prerequisites and considerations:
- The target file (i.e., the online file for which you're checking the version and size) must have a name and URL location that doesn't change with version changes.
- The target file should be moderately sized. You wouldn't want to waste bandwidth downloading a 100MB file every day to compare it with a version you've previously downloaded. For example, the Stinger utility is about 700KB, so its size is ideal.
- The script compares files based on file version and size in bytes. An alternative would be to perform the comparison based on file version alone, but I opted to use both characteristics.
- Understand that some users in your environment might not want to receive the new file as an attachment. They might simply want to know when the file has changed so that they can download it from the source site or from a shared folder location on your internal network.
- If you block .exe file attachments in your internal network, you might want to just send out an email notification and point users to a shared folder that holds the new file.
Getting the Script to Work
I've tested the HTTPDownLoader.bat script on Windows XP Professional Edition Service Pack 1 (SP1) and Win2K SP4 systems. To use HTTPDownLoader.bat in your environment, you first need to create FBlat.txt, which contains the text for the email notification. You then need to perform the following steps to configure the code that Listing 3 shows:
- Configure the Tools variable with the pathname of the folder in which the filever.exe, blat.exe, and downloader.exe tools reside. Don't use double quotes or spaces in this path.
- Set the HTTPloc variable to the Web location of the file you want to retrieve.
- Configure the Notes variable with the Web location of the release notes for the file.
- Configure the Recipients variable with the recipients who require only email notification (i.e., no file attached). Separate recipients by using commas with no spaces.
- Set the FRecipients variable to the recipients who require an email notification with the file attached. Again, use commas with no spaces to separate recipients.
- Configure the PRecipients variable with the recipients who should receive an email notification if the file download fails. An example of such a recipient would be the script administrator or another point of contact who needs to know that the URL might have changed. Again, use a comma-separated list.
- Set the From variable to the email address of the person sending the email notifications.
- Configure the SMTPsrvr variable with the address of the SMTP mail server.
No further configuration to HTTPDownLoader.bat is necessary unless you plan to copy the new file to a share location or you need to specify individual locations for the utilities.
HTTPDownLoader.bat automatically determines the folder in which it resides, then assumes that this folder is where you want to store downloaded file versions and temporary Blat files. Unless you have different storage plans, you won't need to configure a location for these items. If you want to transfer a copy of the new file from this directory to a shared folder location, you'll need to add a line of code to the script's Mailit section, following the instructions in the commented code in Listing 2.
Final Tasks
After you get everything working and send some test messages to yourself, you can add in all your recipients and add this script as a job in Task Scheduler. Contact any support people that might need the file you're downloading—
in this case, the stinger.exe file—
and add them to the distribution list (DL). Keep your eyes open for any other files that might need similar periodic version checks, and let your automated HTTP download scripts do the work for you.