CLI email using Blat and SSMTP

CLI email using Blat and SSMTP

When you are away from your computer, how do you know it is operating as expected? Some might use a web front end to allow them a way to periodically check it, but that involves taking a few moments to do. Instead of the person initiating the check, how about having the machine initiate its status. One way to do this is using email as the transport method.

In this post I’ll cover two command line email tools that will send email from the command line. The first is Blat, which runs on Windows based computers. The next is SSMTP, which runs on Linux based computers. These tools are simple to use, but require some up front work to get them to operate as expected.

First you’ll want to get your email host account settings. This information is what you use to setup email on clients like Outlook, Thunderbird, or your smart phone. Some email hosts have help pages that provide those details, refer to your provider, I will not provide that here.

Now you should have things like.

  • User login
  • Password
  • SMTP server
  • SMTP port
  • Encyrption

We’ll need that when we use Blat or SSMTP. Let’s start with SSMTP. If you haven’t already, install it using this command.

sudo apt-get install mutt ssmtp mailutils

I’ve added Mutt and Mailutils because I also want additional functionality that I won’t cover here, that will be for future posts. You can leave them out if you prefer, but you’ll need them later if you plan to follow me later.

Next, I make a backup of my SSMTP configuration using this command.

cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.bak

Now I’m ready to edit my config file so that my email providers settings are correct. Here is an example

root=admin@email.com
mailhub=smtp.email.com:port
AuthMethod=LOGIN
AuthUser=admin@email.com
AuthPass=password
UseTLS=YES/NO
UseSTARTTLS=YES/NO
FromLineOverride=YES/NO

Next we’ll edit our reverse aliases so that we have the correct senders email.

sudo nano /etc/ssmtp/revaliases

Here is an example that follows my format above.

root:admin@email.com:smtp.email.com:port

Before we go further let’s talk about security, because your email password is stored as clear text in /etc/ssmtp/ssmtp.conf. It is important to secure that file. Refer to this site for details or search for “Secure ssmtp.conf”

https://wiki.archlinux.org/index.php/SSMTP

Now you should be ready to send a message with SSMTP. Type the following command in the terminal, replace the email address below with yours.

du -sh | mail -s “disk usage” admin@email.com

If all is working, you should see a message with the subject “disk usage”. If you hadn’t noticed, I’m piping a standard output from the terminal into the body of my email. This is a useful tool indeed.

For Windows machines, we’ll use Blat. It can be downloaded from http://www.blat.net.

Once downloaded, to use the settings for your account, use this command in a command prompt. Be sure your path to the blat executable works.

blat -server (FQDN or IP of your mail server) -port (TCP port of your mail server) -u (Your email account name) -pw (Your email account password) -subject “This is the subject of your email” -body “This is the text inside the body of your email message.” -attach (This Is Your Attached File.txt) -to (This is who you are sending it to) -f (This is who you are saying it is from)

That should cover the basics of command line email. If you have automated scripts or other process that run, having a way to notify the status is useful when you’re away from your computer. In addition, it is a remote logging function. It can be useful to have a paper trail.

I hope you have found this information useful and look forward to stepping away the system admin stuff for awhile.

Comments are closed.