Send email from the command line

Two weeks ago, I wrote about backing up a remote windows machine using Remote Desktop.

One thing lacking from this solution is email notification. How do we know if the backup job started? Or finished?

There is a free and open source tool that can be used to solve this problem, it is called blat. It consists of an executable, and a DLL.

You can find it at http://www.blat.net/

Send Mail Command Line

Download it, and extract the files to a place where your scripts run. In my case, I put it in c:\backups

To start, you will need an SMTP server that you have access to. That is probably the most difficult part of getting it to work – which means it is very easy to use.

If I just want to send a simple email that tells me a batch script has started, I could call it like this:

blat.exe -f batch@intelliadmin.com -to support@intelliadmin.com -server 192.168.1.1 -subject "Batch file started" -body "The very important batch script has started"

You have five required options to get your email to send:

-f [FROM] (This is the email address that the message will come from)
-to [TO] (This is the email address the message will be sent to)
-subject [SUBJECT] (Subject of the email. Uses quotes if you have spaces)
-body [BODY] (Body of email. Use quotes if you have spaces)
-server [SERVER] (IP Address or host name of the server)

That is for a simple message. Blat is filled with powerful options. For example, I could run a robocopy in my script, and redirect the output to a text file like this:

robocopy c:\backup \\server\c\backup >> output.txt

Then, when I send my notification I could attach the output text file to my email like this:

blat.exe -f batch@intelliadmin.com -to support@intelliadmin.com -server 192.168.1.1 -subject "Batch file started" -body "The very important batch script has completed" -attach c:\backup\output.txt

Once the command runs, the output will show up as an attachment in my inbox:

Send email cmd prompt

Like I said, it is loaded with options. Check it out, and when you do just type blat.exe -help to get the entire list of command line parameters.

Original Article from www.networksteve.com

Filed Under: BAT Files, Tools, Utility

Comments

  1. Jennifer Smathers says:

    Great tip Steve, I never knew that utility existed. Thanks

Leave a Reply

Please wait while we add your email address to our list

Join our Tips and Software Email List

Get free software, news, and tips
once a week by joining our email list
Enter your email address below

Just like you, we hate spam.

We promise to never sell, or spam your inbox

Thanks for joining our list, and have a great day!

Error adding address

There was an error adding your email address.
It might be because you are already on our list.
If this is not the case, please try again later. Thanks!