In a previous recipe, we learned how to install and configure Postfix as our domain-wide e-mail server. When it comes to working with e-mails, there are lots of different tools and programs available for Linux and we already showed you how to send e-mails through the sendmail
program as well as the swaks
utility. Here in this recipe, we will show you how to work with one of the most commonly used mail utilities in Unix and Linux, called mailx
, which has some useful features missing in the sendmail package for sending mails or reading your mailbox.
We will begin this recipe by installing the mailx
package on our server running our domain-wide Postfix service, as it is not available on CentOS 7 by default.
yum install mailx
mailx
with its standard input mode, as follows:echo "this is the mail body." | mail -s "subject" [email protected]
mailx
command from a shell script, using multiple recipients, or attaching some files to the e-mail:cat ~/.bashrc | mail -s "Content of roots bashrc file" john echo "another mail body" | mail -s "body" john,[email protected],chris echo "this is the email body" | mailx -s "another testmail but with attachment" -a "/path/to/file1" -a "/path/to/another/file" [email protected]
One big advantage over the
sendmail
program is that we can use mailx
to directly connect to and communicate with remote MTA mail servers. In order to test this feature, log in to another Linux-based computer, which should be in the same network as our Postfix server, install the mailx
package, and send a mail through our Postfix server's IP address 192.168.1.100
(we have already opened the incoming SMTP firewall port in a previous recipe). In our example, we will send a local mail to the user john
:
echo "This is the body" | mail -S smtp=192.168.1.100 -s "This is a remote test" -v [email protected]
Not only can the mail
x
program send e-mail messages to any SMTP server, it also provides a convenient mail reader interface for your local mailbox when started locally on the Postfix server. If you run the mail program with -f
specifying a user mailbox, the program will start by showing you all the inbox e-mails. But remember that mailx
can only read local mailboxes when the program is started on the same server your mailboxes are located at (if you want to use it to access your mailbox remotely you need to install an MTA access agent such as Dovecot—see later—with POP3 or IMAP). For example, login as Linux system user john
on the Postfix server, and then, to open the mail reader with your user's local mailbox, type: mailx -f ~/Maildir
.
You will now be presented with a list of all the mail messages in your current inbox. If you want to read a specific mail, you need to type in its number and press the Return key. After reading it, you can type d followed by Return to delete it or r followed by Return to reply to it. To go back to your current mail message overview screen, type z followed by Return. If you have more than one screen of mail messages, type z- (z minus) followed by Return to go back one page. Type x followed by Return to exit the program. To learn more, refer to the mailx
manual (man mailx
).
In this recipe, we showed you how to install and use mailx
, a program to send and read your Internet mail. It is based on an old Unix mail program called Berkely mail and provides the functionality of the POSIX mailx
command. It should be installed on every serious CentOS 7 server because it has some advantages over the sendmail
program and understands the protocols IMAP, POP3, and SMTP (If you need an even more user-friendly mail reader and sender, you can check out mutt. Type yum install mutt
to install it. Then type man mutt
to read its manual).
So what did we learn from this experience?
We started this recipe by installing the mailx
package using the YUM package manager on our Postfix server. It includes the mailx
command line program which can be run either with the command mail
or mailx
. Afterwards, we ran the program with the -s
parameter, which specifies an e-mail subject and; also you need a recipient e-mail address as argument, either an external address or a local Linux system user name or mail. Without anything else, mailx
suspects it's running on the same server as the mail server is on, so it implicitly sends the mail to the localhost MTA, which is Postfix in our example. Also, in its most simple form, mailx
starts in interactive mode, which lets you type in the message body fields manually at the command line. This is good for quickly writing a mail for testing, but in most cases you will use mailx
by piping in content from another source. Here we showed you how to do this by using the echo
command to write a string to the Standard Input (STDIN) of mailx,
but you can also cat
a file content into it.
One often used example is to send some kind of file output or a log file content of a failing command to an administrator user or system reports at a certain scheduled time point using cron
. Afterwards, we saw that we could also send mails to multiple recipients by comma-separating their e-mail addresses, and showed you how to send attachments along with your mail messages by using the -a
option. In the next section, we then showed you how to send mails to a remote SMTP mail server using the -S
option to set internal options (variable=value
). This is a very useful feature if you haven't specified your standard mail server on your DNS server or for testing a remote mail server. Finally, in the last section we showed you how you could read your local mailbox on your Postfix server using mailx
. It has a convenient browsing functionality to read, delete, and reply, and do advanced e-mail management for your local mailbox. You do this by typing in commands into the mailx
interactive sessions followed by pressing the Return key. Remember, if you don't like this way of browsing your mails, you can also always read or filter your mails in your user's ~/Maildir
directory using command-line tools, such as grep
, less,
and so on. For example, to search all new mails for the case-intensive keyword PackPub.com,
type grep -i packtpub ~/Maildir/new
.