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 firstname.lastname@example.org). 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:
- -a filename: Allows you to add a file as an attachment to your message.
- -c cc-addr: Specifies the name of a user you want to send a copy of the message to.
- -b bcc-addr: Sends a blind copy to a user. The recipient of the mail cannot see that you’ve sent a copy to this user also.
- -R reply-addr: Allows you to specify the reply address. A reply to this mail message is automatically sent to this reply address.
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 email@example.com Wed Nov 19 15:13 20/661 Meeting at 10 AM U 2 firstname.lastname@example.org Fri Nov 21 09:48 20/661 10 AM meeting cancelled U 3 email@example.com Fri Dec 5 10:44 20/661 Nice day for Dutch users U 4 firstname.lastname@example.org Fri Dec 5 12:28 19/568 hello U 5 email@example.com 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