Mail and Mailx



Many Linux users have probably noticed the /var/spool/mail/ directory (or /var/mail/ on some systems). However, what is it, why is it used, and how do we use it? Well, these directories are for the Linux (or Unix) mailing system. Typically, most systems use the “mail” command, but alternatives are available.

The "mail" command in a terminal allows Linux users to send mail to another user. The "-s" parameter is used to set a subject and "-u" declares a user (the recipient).

The "mail" command alone, with no parameters, allows users to check their mailbox (list received emails). To read the third email, type "3" and then press "Enter". After reading received mail, press "d" to delete the message or "r" to reply. Pressing "q" will close the mail program. When viewing an email, pressing "+" will display the next email (the fourth message), and pressing "-" will display the previous message (the second email).

Users can send messages to other users using a command similar one of the below lines.

echo "Your message" | mail -s "Subject" [email protected]
mail -s "Subject" [email protected] > email-file.txt

Alternately, users can type the mail command with the recipient address and subject and then press "Enter" without using pipes or redirections. Then, the user can type the email and press "ctrl+d" when finished.

To send a message to multiple users, use the "-c" parameter and then list the recipients.

To send a message to a user on the same system, type their user name and the localhost name for the system. For instance, in a terminal, type "hostname". Then, when typing the recipient, type the user's name ("bill", for example) and the hostname with and “@” separating the two. So, if the hostname is "mylaptop", then the recipient address would be "bill@mylaptop". Alternately, use grave quotes [`] (the quote on the tilde button) to surround/enclose the "hostname" command that will serve as the hostname if you do not know it. For instance, instead of typing "bill@mylaptop", a user could type "bill@`hostname`". In the shell, the command in grave quotes are executed first. Because "hostname" returns the system's hostname, "bill@`hostname`" becomes "bill@mylaptop".

Alternatives to the "mail" command include "mailx" and "sendmail" which use similar parameters and concepts. Mailx is more advanced than “mail”. Mailx supports attachments by using the “-a” parameter. Users then list a file path after the “-a” parameter. Mailx also supports POP3, SMTP, IMAP, and MIME.

By using the cron tables, users could make scripts that will email users scheduled messages of reminders. A private company could use “mail”, “mailx”, or some alternative to provide an internal email service provider.

Further reading




  • slide.jpg
    40.6 KB · Views: 34,804

Members online