Command line tricks - sending yourself email

It might sound like a rather strange thing to do, but sending yourself email from the command line can prove extremely handy in a lot of circumstances. For example, I have a script that runs every week on my server that emails me and reminds me to copy the latest backups off that actual machine (in case of hard drive failure).

Now there are certain prerequsites for this process, namely having a working installation of a mail transfer system on your Unix-like operating system. Most Linux distros, for example, have sendmail installed and configured out of the box such that this should 'just work', but in some cases it might not.

For the most part, sending mail is as easy as using the mail command.

Let's give an example of how you might use it:

$ mail -s Subject whoever@wherever.com

After you hit enter, you then need to type in your desired message and press Enter followed by Ctrl-D to send it off when you're done. At this point, you might also be asked about the Cc settings, but you can just leave that blank and hit Enter to send away!

You don't actually have to provide the -s switch for the subject by the way; if you just do mail whoever@wherever.com in most cases, it will just ask you for the subject first and then after confirming that, you'll need to enter the body of the message.

It doesn't have to be an internet email address either, you can send mail to users on your machine in most cases by just entering that username (for example, mail root). Beware that if it is an internet email address and your machine is on a dynamic IP address (like most residential internet connections), you may find your emails getting caught in spam filters. Sadly, there's no real way to avoid this without things getting too complicated.

Of course, if you're suitably trained in the art of Unix command line hackery, you can do all sorts of more advanced things, like piping the output of one command into mail for use in a script or for a very complex job. A great example of this is my backup reminder script I mentioned earlier. Let's take a look:

#!/bin/sh

DIR=/my/backup/dir

FILELIST=""

for each in `ls -1t "${DIR}" | head -n 21`; do
FILELIST="${FILELIST}${each}\n"

done

MAIL="Hey there.\n\nIt's that time again, time to copy some backups off-site.\n\nThe following backups are from this week:\n\n${FILELIST}\n\nEnjoy!"

echo -en ${MAIL} | mail myemail@ddress.com -s "New backups"

What this does is it emails me the list of the last 21 files in the backup directory (sorted by date), which is roughly how many get created in a week. So every week, I get a notification of what I need to move off that machine!

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Command line tricks - sending yourself email

Home » Articles » Command line tricks - sending yourself email