Easily Send Command Line Emails with Blat [Windows]

blatlogo   Easily Send Command Line Emails with Blat [Windows]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.

blat11   Easily Send Command Line Emails with Blat [Windows]

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”

That’s it!

blat21   Easily Send Command Line Emails with Blat [Windows]

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.

blat31   Easily Send Command Line Emails with Blat [Windows]

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.

blat4   Easily Send Command Line Emails with Blat [Windows]

Currently, when the script encounters a problem pinging any of the sites, it’ll issue an alert window that looks like this.

blat5   Easily Send Command Line Emails with Blat [Windows]

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 rdxxxxx@gmail.com -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.

blat6   Easily Send Command Line Emails with Blat [Windows]

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

The comments were closed because the article is more than 180 days old.

If you have any questions related to what's mentioned in the article or need help with any computer issue, ask it on MakeUseOf Answers—We and our community will be more than happy to help.

5 Comments -

Tim

You can even run Blat without the install by putting all the options you would put in install on your command line.

If part of your command line needs spaces, you can enclose that part (ie subject) in quotes.

Also, in your scripting example, if you put the “strFailedList” string in quotes and separated the individual sites with | instead of -, it would be a list with one site per line.

Finally, support can be found in the Blat Yahoo Group.

Ryan Dube

Awesome tips – thank you Tim!

dylan

Isn’t it a bit confusing? You know how about those that doesn’t have enough knowledge in command prompt?

Tim Bare

For what it’s worth, I also wrote an app for this – SwithMail – i find it quite a bit easier to use than blat (which is why i wrote it, actually…) It’s got a nice gui for getting your strings together, has XML support for different templates (that modifiable parameters in the body that you can specify from the CLI, etc…

has many options Blat doesn’t, too, including SSL support. you can check it out @ http://swithmail.tbare.com – and any suggestions are welcome on my forum: http://forum.tbare.com

Great tutorial for Blat, by the way. :)

Ryan Dube

Thanks Tim! I’ve always felt it would be really nice if there was some sort of GUI to simplify the process. Thanks for posting your app, I’ll have to check it out.