Easily Send Command Line Emails with Blat
Blat. Not exactly the word you envision would be the name of a tool that you can use to send out emails to anyone in the world, from any application or software tool that you desire. It more sounds like the sound that a raw egg makes when it hits the floor.
Well, that’s exactly what I’m going to introduce to you today, if you’ve never heard of it before. Blat is not new, but it’s important. There are numerous applications and utilities that will automatically send email notifications out for you.
You can get RSS news updates automatically emailed. You can have websites send you email notifications, like the 10 notification sites that Saikat covered. You can even set up services like Google Analytics to send you automatic reports every now and then.
But what if you want to set up batch jobs or Windows scripts on your own computer where you can send out emails straight from the command line? Well, Blat is a Win32 utility that you can use to send emails via SMTP straight from the command line.
Setting Up Blat Command Line Emails
Blat is a free utility that’s been available ever since Windows 95, and it has continued to evolve through the years. It is now available as a SourceForge offering.
While Unix users have utilities like sendmail to issue command line emails, Windows users have turned to Blat since Windows 95 to do the same. Many computer users don’t realize how easy it can be to send emails via the command line in Windows, only because they’ve never tried blat. So, for those of you that have never seen it, I would like to introduce you to this brilliantly simple, yet powerful command line utility.
After you download and extract the Blat files, you’ll find blat.dll, blat.lib and blat.exe. Make sure to place them all in one folder, and you can launch the .exe file from there.
The first step is to issue the “install” command which will tell blat what SMTP server and account to use.
In this case, the command I issued was “blat -install <smtp server> <email user id>”
This works for my SMTP account (no password required), but if yours requires authentication, you can include “-u <username> -pw <password>”, but keep in mind that blat still doesn’t support SSL without some third party tool, so it won’t work at this time with Gmail SMTP. Once you’ve got the id/pw and smtp server to use installed, you can issue the blat command at any time from the command line.
In my example, I have a file stored in the blat directory called help.txt that contains all of the blat command parameters. I want to email this text file as the email body using blat. I can do this in one command by typing “blat help.txt -to <email address to send to> -subject TestFile”
This command opens up the test file, embeds it as the email body, uses the subject line that you’ve defined (don’t use any spaces though), and then sends the email to the recipient you defined. Here’s what the email looked like when I received it.
While being able to open up a command prompt and issue a quick email like this is pretty cool, it’s even cooler to use that ability from within an even more powerful Windows Script.
Issue The Blat Command From Within A Windows Script File
My goal in this example is fairly simple. I have a Windows Script that pings a list of websites that I have listed in a text .ini file.
Currently, when the script encounters a problem pinging any of the sites, it’ll issue an alert window that looks like this.
Instead, I’m going to improve my script by issue an email whenever a website isn’t pingable, rather than depending on a pop-up window. To do this, I’m just going to add the following section to my existing Windows Script.
'If any ping failed, send an email If Ping = false then '* Send out email wscript.echo "Sending " & strFailedList Shell.run "c:\temp\blat\blat.exe -body " & strFailedList & " -to firstname.lastname@example.org -subject TestFile" End If
If you’ve never used scripting in WSF before, you have to issue the command “Set Shell = wscript.createObject(“wscript.shell”)” to set it up. Then you can use Shell.run as show above to launch the blat.exe command.
A few hard lessons learned about blat limitations. The command above only works if the string variable ‘strFailedList’ has no spaces in it. To accomplish this I use a dash between each failed IP ping, so my final email looks like this.
If you want a better formatted email, you could always output any messages that you want to email out into an output file, and then replace the “-body” parameter with the name of the file instead (like the example I showed in the first half of this article).
As you can see, the blat tool lets you integrate email sending into your Windows Scripts, and opens up the possibility of all sorts of useful email notifications for when things go wrong with your PC.
If you’ve never tried it before, give the Blat tool a test and let us know what you think. Did you come up with any creative uses for it in your Windows scripts? Share your ideas and insight in the comments section below.
Image Credit: ShutterStock