comp.org.uk

Networking | Programming | Security | Linux | Computer Science | About

Send Mail from the Command Line

You may think that in order to send mail, you need a full-scale mail client, such as Thunderbird, Evolution, or Windows Mail. The Linux command line, however, also has a mail client, which you can invoke from the command line by using the mail command. I wouldn’t recommend replacing your normal mail client by mail, but if you want to send a message to an Internet or local user, or if you want to read system mail, the mail command offers an excellent solution to do that.

To send a mail message to another user, you invoke the command as mail user, where user is the name of a local user (e.g., mail root) or a user on the Internet (e.g., mail someone@example.com). Next, the mail program opens an interface where you first enter the subject, followed by the body of the mail message. When finished writing the mail body, you type a dot on a separate line and press Enter. This will tell the mail client that you’re done and offer the mail message to the SMTP process on your machine, which will take care of delivering it to the correct user.


Note: You can use the mail command to send mail to Internet users, but this requires DNS to be set up properly on your Linux machine and an SMTP process running. Notice that Ubuntu does not install smtp by default, if you want to use mail on Ubuntu, install it first using apt-get install dhcpclient.


Below, you can see what happens when using the mail command from the command line.

comp:~ # mail linda 
Subject: 4 PM meeting 
Hi Linda, can we meet at 4 PM? Thanks, root 
. 
EOT

You can also run the mail utility completely from the command line, without it opening an interface that has you input text. This, for example, is very useful if you want shell scripts or scheduled jobs to send a message automatically if a certain error condition occurs. In these cases, the body of the mail message is not very important; you probably just want to deliver a mail message with a certain subject to the user. The next command shows you how to do this: it sends a message with the text “something is wrong” to the user root. Also, take notice of the < . construction. Normally, the mail command would expect a dot on a line on its own to indicate that the message is complete. By using input redirection with < ., the dot is provided on the command line.

mail -s "something is wrong" root < .

The mail command has some other useful options as well for sending mail:

Apart from sending mail, you can read mail messages also with the mail utility. The utility, however, is meant to read system mail and is not a good choice to read your POP or IMAP mail from the mail server of your Internet provider. When invoking mail to read your system messages, you should just type mail. In reply, the mail client shows a list of mail messages that are waiting for you.

comp:~ # mail 
mailx version nail 11.25 7/29/05. Type ? for help. 
"/var/mail/root": 5 messages 5 unread 
>U 1 root@comp.higgins.b Wed Nov 19 15:13 20/661 Meeting at 10 AM 
 U 2 root@comp.higgins.b Fri Nov 21 09:48 20/661 10 AM meeting cancelled
 U 3 root@comp.higgins.b Fri Dec 5 10:44 20/661 Nice day for Dutch users 
 U 4 root@comp.higgins.b Fri Dec 5 12:28 19/568 hello 
 U 5 root@comp.higgins.b Wed Dec 10 08:48 20/661 Wanna go for coffee? 
 ?

To read a message, just enter the message number, and you will see its text. When finished reading the message, press q to quit. After closing a message that you’ve read, you can type the reply command from within the mail interface to send a reply to the user who sent the message, or type delete, followed by the message number to delete the message from your system. Next, type quit to exit the mail interface.


Published on Tue 24 December 2002 by Nigel Atkins in Linux with tag(s): mail command line