I found when talking about mailx, many tutorials teach you how to use it to send emails. This is ridiculous as the first thing you need to know about an email client is how to use it to read emails. This is especially the case for text-based email client such as mailx.
mailx is not installed on CentOS by default, so to use it, you should install the mailx package first:
1 |
yum install mailx |
Now, you should know where mailx reads emails. A user gets his emails stored in /var/spool/mail/userlogin. So, if you login as root, your emails are saved in the single file /var/spool/mail/root. This is a text file so you can use vi/cat, etc. to read it if you have not installed mailx. Using mailx is just a more convenient way to read your emails.
Type the command mailx, you will be dazzled by a screen of information. But do not care about what is on the screen now except the last line. There is only one character & at the beginning of last line which is a prompt( like similar one on the shell command line) to hint that you can input a command here. You can type “help” and press enter to get available commands and their usage.
Now, we look at the main part of the screen. It is a list of email headers. Email header is summary of an email including who sent you what email(subject line) at when. An email header also shows the number of lines/number of characters in the email. Pay attention to the first character of email headers. In most cases, it is N,U,or nothing. N means new email, i.e., the newly incoming emails since you ran the mailx command last time. So a N email will become a U email when you quit mailx and run mailx again. U means an unread email. If you have read an email, the U flag will disappear, even if you quit mailx and run mailx next time. How does mailx remember the status of an email? Well, mailx writes the status character directly into the email file(i.e., /var/spool/mail/root) at the “Status:xxxx” line of the email, so when it is run next time, it will read the status line and show the status flag at the beginning of headers.
Now it is time to read some emails. There is a concept of current email(message) which is indicated by a “>” at the beginning of a header. Press enter at the prompt line will open the current message for you to read. You can press the space key to see next page during reading the content of a message. As said before, the U flag will disappear from email header after you read its content. The second field of an email header is the number of the message. You can use “p xxx” to read the xxx message. You can use “d xxx” to delete message xxx. You can use “d xxx-yyy” to delete all emails numbered from xxx to yyy. The list of emails is missing after you read some message. To recall the list of email headers, type “h” or “headers” and press enter at the prompt line. There are only 20 emails headers displayed on the screen. To see other email headers, type “from xxx-yyy”.
Type “quit” to quit mailx. You can also type “exit” to quit mailx but this won’t save the status (such as you’ve read/deleted what messages during this run of mailx) to the email file so next time you run mailx, you will find the emails you deleted are still there or the emails you have read are still indicated as unread.