Linux mail - a Linux shell script to send mail

Here's a n example Linux shell script (Bourne shell to be specific) that I use to send a list of directories to one of our invoicers. She uses this list as part of a cross-checking process to make sure she bills each one of our customers who have a directory allocated to them. The list is sent to her automatically from a Linux crontab entry I created for her.

Without any further ado, here is the Linux shell script that sends the email. (Note that although I keep writing "Linux mail", this should also work on other Unix systems.)

Example Linux shell script - sending mail

Here's the source code for the Linux shell script that sends the email message out every day:

#!/bin/sh

cd /home/alexander/bin

echo "

DO NOT REPLY TO THIS EMAIL MESSAGE.

This is a list of the current customer directories installed on our server.
Please make sure they are all billed.

"                       >  listOfWebSites
ls -1 /customers        >> listOfWebSites

mail -s "Current customer directories on our server" kim@herdomain.com < listOfWebSites

That script creates a file named listOfWebSites, then sends that file via the standard Linux mail command.

Corresponding Linux crontab entry

Here is the crontab entry that I use to send this email message every day. If you're familiar with the crontab format you should be able to easily figure out the date and time this information is mailed to here.

30 7 12 * * /home/alexander/bin/mailWebSitesToKim

(If you need it, here's a link to our online Linux crontab man page.)

I thought this was a pretty decent use of shell scripting and the built-in Linux mail command. Notice how easy the Unix operating system and redirection operators make this.

I hope this script can help you, or even prompt you to do something similar to this for your own needs.

Permalink

Very useful mate!

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.