Have you ever needed to email the output file from a script or scheduled task, but the tool didn’t come with its own email functionality? I found myself in exactly this situation the other day, and reached for my trusty emailing command line tool, blat.
The website sums up what this SourceForge project is simply and succinctly; Blat is a Win32 command line utility that sends email using SMTP or post to UseNet using NNTP. While Blat can do NNTP as well, it has been helping Windows admins email enable their scripts, utilities, and home grown apps for years, and that’s what I am going to talk about in this post. With a simple command line syntax that can be called interactively or from a batch file, blat can generate an SMTP message and send it on its way with practically any options you might want.
Blat can run from a command line (interactively or in a batch file) or can be installed as a service. When installed, you can define various profiles of settings that are stored in the registry and can be used repeatedly.
When you call blat from your batch file or execute it interactively, you feed it a series of switches to define things including the subject, what text you would like to place in the body, any file you want to attach (such as the verbose output from some scheduled task,) the sender address, what relay server to use, and more. To use blat, download the file from SourceForge and extract it. You don’t need to do anything else, as long as you put the executable, LIB, and DLL in your path, or specify their location when you call them.
In my particular case, I have a robocopy job that runs each night and I want to have the log emailed to me after it’s done (because I cannot seem to remember to check the log on the server each day.) At the end of the script, I invoke blat with this command;
blat c:scriptletsmessage.txt -attach g:robocopylog.txt -to email@example.com -s “robocopy log” -server demeter.olympus.home -f firstname.lastname@example.org
Here are the things that blat is doing.
- c:scriptletsmessage.txt contains the text I want to appear in the email.
- -attach g:robocopylog.txt attaches that specific file to the email
- -to email@example.com specifies who to send the email to
- -s “robocopy log” makes that the subject of the email
- -server demeter.olympus.home specifies the SMTP relay server
- -f firstname.lastname@example.org specifies the from address of the email. This is also used for authentication to your relay (if required) so you can specify an alternate reply-to address if necessary.
As you can see from the image at the start of this email, the text contained in the message.txt file is a short one. It could be as long as I want it to be. Blat includes a myriad of other switches, which are documented here.
While there are Perl modules that can send email, and ASP.NET modules that can send email, and if you format a text file with the right fields you can drop in the the pickup folder of an SMTP relay, Blat is quick and easy to use, and just works. Blat can also work with SSL, and authentication requirements like POP3 before SMTP. Consider it the next time you need to send email from something that cannot make a MAPI call.