LOGO

Blat: Send Command Line Emails Easily

January 13, 2012
Blat: Send Command Line Emails Easily

Introducing Blat: A Command-Line Email Tool

The name Blat might not immediately inspire confidence – it doesn't sound like a sophisticated email solution. In fact, it resembles the noise of a broken egg.

What is Blat?

Despite its unusual moniker, Blat is a valuable and surprisingly robust tool. It allows you to dispatch emails from virtually any application or software, offering a level of control often missing in automated notification systems.

While many services offer automated email notifications, such as RSS feed updates or website alerts – as highlighted in previous coverage – Blat provides a different approach.

Why Use Blat?

Consider scenarios where you need to integrate email functionality directly into your own scripts or batch processes. For example, you might want to send automated reports generated by a Windows script.

Blat excels in these situations. It's a Win32 utility specifically designed to send emails via SMTP directly from the command line.

Key Benefits of Using Blat

  • Flexibility: Integrate email sending into custom scripts and applications.
  • Automation: Automate email notifications from batch jobs.
  • Command-Line Interface: Send emails without a graphical user interface.
  • SMTP Support: Utilize standard SMTP protocols for reliable email delivery.

Although not a recent development, Blat remains a significant tool for system administrators and developers seeking a powerful and adaptable email solution.

Utilizing Blat for Command Line Email Sending

Blat is a versatile, no-cost utility that has been a staple for Windows users since the days of Windows 95. Its functionality has been consistently updated and refined over time.

While Unix-based systems benefit from tools like sendmail for command line email dispatch, Windows has long relied on Blat to achieve the same outcome. Many users are unaware of the simplicity of sending emails directly from the Windows command line, simply because they haven't explored Blat's capabilities. Therefore, this article introduces this remarkably straightforward, yet potent, command line application.

Following the download and extraction of the Blat files, you will locate blat.dll, blat.lib, and blat.exe. Ensure these files are all contained within a single folder, from which you can then execute the .exe file.

The initial step involves issuing the "install" command, which configures Blat with the necessary SMTP server and account details.

easily-quickly-send-command-line-emails-blat-1.jpg

In this instance, the command used was "blat -install <smtp server> <email user id>".

This configuration works with my SMTP account, as it doesn't require a password. However, if your account necessitates authentication, you can incorporate "-u <username> -pw <password>". It's important to note that Blat currently lacks native SSL support without the aid of a third-party application, preventing its use with Gmail SMTP at this time. Once the ID/PW and SMTP server are installed, the Blat command can be invoked from the command line whenever needed.

I have a file named help.txt, stored within the Blat directory, containing a comprehensive list of Blat command parameters. I intend to send this file's content as the email body using Blat. This can be accomplished with a single command: "blat help.txt -to <email address to send to> -subject TestFile".

That concludes the process!

easily-quickly-send-command-line-emails-blat-2.jpg

This command accesses the test file, incorporates its content as the email body, utilizes the specified subject line (avoiding spaces), and dispatches the email to the designated recipient.

easily-quickly-send-command-line-emails-blat-3.jpg

The ability to quickly send an email from the command prompt is valuable, but it becomes even more powerful when integrated into a Windows Script.

Executing the Blat Command Within a Windows Script File

The objective of this example is straightforward. I have a Windows Script that tests the connectivity of a list of websites, as defined in a .ini text file.

easily-quickly-send-command-line-emails-blat-4.jpg

Currently, if the script encounters an issue pinging any of the sites, it displays an alert window similar to the one shown.

easily-quickly-send-command-line-emails-blat-5.jpg

I am enhancing the script to send an email notification whenever a website is unreachable, instead of relying on a pop-up window. This is achieved by adding the following section to the 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 are unfamiliar with WSF scripting, you must first initialize the shell object using "Set Shell = wscript.createObject("wscript.shell")". Then, you can utilize Shell.run as demonstrated to launch the blat.exe command.

A key limitation of Blat has been observed. The command above functions correctly only if the string variable 'strFailedList' contains no spaces. To circumvent this, I use a dash to separate each failed IP address in the email, resulting in the displayed email format.

easily-quickly-send-command-line-emails-blat-6.jpg

For a more structured email, you can direct any messages intended for email to an output file and then substitute the file name for the "-body" parameter (as illustrated earlier in this article).

As demonstrated, Blat enables the integration of email sending into Windows Scripts, facilitating the creation of useful email notifications for system events.

If you haven't explored Blat, we encourage you to test it and share your experiences. Have you devised any innovative applications for it within your Windows scripts? Please share your ideas and insights in the comments below.

Image Credit: ShutterStock

#command line email#blat#email utility#send email#command line#windows