LINUXUSER Command Line: Mailx Hao Wang, Fotolia

Mail at the command line Leaner Letter

The simple mailx command-line client handles mail either interactively or via command-line options. Although it lacks the convenience of a GUI-based tool, mailx compares well in scripting.

By Heike Jurzik

hunderbird, Evolution, or Kmail bin/mail. The following commands and sage. The Enter key also scrolls down, are not the only options as mail command options were all tested on although you can use the up and down Tclients. Although the major ­Debian 4.0 “Etch.” arrows, too. Pressing Q quits the display graphical mail clients offer users a great Launching the mail client with mailx and takes you back to the main screen. deal of convenience, sometimes a com- at the shell prompt takes you into an in- If the messages have scrolled out of the mand-line solution is an advantage – teractive environment. The client shows window, you can press H and Enter to both [1] and the lean mailx [2] pro- the content of your inbox; the default display them again. gram support batch options in the shell. setting for this on most systems is /var/ Mailx has two approaches to terminat- Most distributions include mailx by mail/username. Then you will see an ing the program: pressing X and Enter default. OpenSUSE and Debian include overview of the messages, including the quits the mail client without removing the package on their installation media. sender, date, size, and subject line. messages you have read from the inbox, On openSUSE, /bin/mail is a symlink Typing ? and pressing Enter outputs and Q plus Enter does the following: that points to /usr/bin/mailx, whereas a quick help file about using the mailer. Debian systems do things the other way Entering the number and pressing Enter Saved 2 messages in /home/petros around: /usr/bin/mailx points to /usr/ is the the easiest way to display a mes- ilie/

90 ISSUE 91 JUNE 2008 Command Line: Mailx Linuxuser

Held 3 messages in /var/mail/pet rosilie

Typing D and pressing Enter deletes messages. Again, you can select mes- sages to delete by specifying their num- bers. For example, d 1 3 deletes the first and third messages. Also, you can select a range, as in d 1-10, which deletes the first 10 messages, or d *, which deletes all messages. If you mistakenly delete a message, u will undelete it. The com- mand uses the same combinations of digits as the d option. Entering R lets you answer a message, and pressing M starts a new message. To specify the target ad- Figure 2: Mutt also comes with a rudimentary GUI, although the keyboard-driven menu dress, use the m command: ­system isn't much different from working at the command line.

m [email protected] ates the $VISUAL environmental vari- sent a status report, which then turns able to identify your favorite editor. If up a short time later in the inbox. The Mailx then prompts you to enter the sub- you like to work with, for example, Joe, “Using Mailx Options” box shows an ject line before you can move on to the you can set up the editor for mailx by ­example of the use of mailx options. message itself. entering export VISUAL=joe. Using uuencode is the only way to add When you are done, type a period fol- Mailx shows its strength, flexibility, an attachment to a message. This tool lowed by Enter to add more recipients and speed in the shell. The generic com- converts binary files, such as images or after Cc: (carbon copy): mand line is: executables, into printable ASCII charac- ters. After doing so, the text file can be Cc:[email protected],[email protected] mailx [Option] [email protected] mailed (see the “Sending Attachments” box). The recipient then sees the file as If you prefer to launch your favorite edi- Users can define options, such as the a text block between the keywords begin tor to compose messages, type ~v di- subject line (‑s), the mail address for and end. Uudecode converts the file rectly after the subject line. Mailx evalu- the bcc (blind carbon copy) field (‑b), back. The command uudecode attach_ further recipients to cc (‑c), and so on. image.txt results in the file hej.jpg. Using Mailx Options Don’t forget to use quotes for blanks and Things are easier if you run Mutt in Sending a copy non-standard characters. batch mode. The Mutt mail client not The mail body – that is, the message only offers ‑s for subject, ‑c for copy, and nonumber text – is still missing. For a short mes- -b for blind copy options referred to in $ mailx ‑s 'Hello!' petronella@ sage, echo is probably a good choice. this article, but also the ‑a parameter huhnix.org ‑c petrosilie@huhnix. Swapping the body text out to a file for simple attachment sending. See the org [...] makes sense for longer texts. Mailx “Sending Attachments” box. n Message with echo can read the file from standard input. nonumber If you need to troubleshoot your INFO setup, -v is a big help. On Debian sys- $ echo 'Hello, this is a test.' | [1] Mutt: http://www.​­ mutt.​­ org/​­ tems, mailx just tells the user that it has mailx ‑s 'Hello!' petronella@ [2] Mailx: http://www.​­ scit.​­ wlv.​­ ac.​­ uk/​­ huhnix.org ‑c petrosilie@huhnix. ​­cgi‑bin/mansec?​­ 1+mailx​­ org Sending Attachments Swapping out a message text Attachments with uuencode and mailx Heike Jurzik studied nonumber nonumber German, Computer $ mailx ‑s 'Hello!' petronella@ Science and English $ uuencode hej.jpg hej.jpg > huhnix.org < mail.txt at the University of attach_image.txt Cologne, Germany. Troubleshooting via status report $ mailx ‑s 'photo' petronella@ She discovered nonumber huhnix.org < attach_image.txt Linux in 1996 and has been fascinated $ echo 'Hello, this is a Attachments with Mutt with the scope of the Linux com- test.' | mailx ‑v ‑s 'Hello!' nonumber

THE AUTHOR THE mand line ever since. In her leisure [email protected] $ mutt ‑s 'Photo' ‑a hej.jpg time you might find Heike hanging Mail Delivery Status Report will out at Irish folk sessions or visiting [email protected] < mail.txt be mailed to . ­Ireland.

JUNE 2008 ISSUE 91 91