How to Email Enable your Scripts

blatHave 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:\scriptlets\message.txt -attach g:\robocopylog.txt -to -s “robocopy log” -server demeter.olympus.home -f zeus@olympus.home

Here are the things that blat is doing.

  • c:\scriptlets\message.txt contains the text I want to appear in the email.
  • -attach g:\robocopylog.txt attaches that specific file to the email
  • -to 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 zeus@olympus.home 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.

Written by Ed Fisher

An InfoTech professional, aficionado of capsaicin, and Coffea canephora (but not together,) I’ve been getting my geek on full-time since 1993, and have worked with information technology in some capacity since 1986. Stated simply, if you need to get information securely from a to b, I’m your guy. I’m like "The Transporter," but for data, and without the car. And with a little more hair.

1 Comment

  1. Chris Jones · March 2, 2011

    Aren’t there inherent security risks in sending an email like that depending how detailed your log is?

    It would be nice to check things like that from my Blackberry, but I wouldn’t take that chance personally.

Leave A Reply