Create and read emails using the MAILX command in Linux

Home > Search > Linux commands

Use apt-get or yum to install the mailx.

[root@server1 ~]# yum install mailx


The mailx command with no options can be used to view the number of emails in your mail directory, and to also read the emails.

~]# mailx
Heirloom mail version 12.5.7/5/10. Type ? for help.
"/home/john.doe/Maildir/": 2 new messages 2 unread
>U jane.doe              Mon Jan 01 09:00 17/565  "Hello John"
 U jack.doe              Mon Jan 01 10:00 17/532  "Hello dad"


The mailx command can be used to send an email. In this example, an email is sent to This assumes there is a user account for john.doe on the email server. In this example, after typing "Hello John", press Ctrl + D to send the email.

[root@server1 ~]# mailx
Subject: Example subject
Hello John



The -s option can be used to include the subject in the command line, so that you are not prompted for the subject.

[root@server1 ~]# mailx -s "Example subject"


Message Body

Echo can be used to add a message in the command line, so that you are not prompted for the message.

[root@server1 ~]# echo "Example message" | mailx


Carbon copy

The -c option can be used to carbon copy.

[root@server1 ~]# mailx -c


The -b option can be used to blind carbon copy.

[root@server1 ~]# mailx -b


Default options

The -A option can be used to send an email with default settings. For example, if the email server is configured to require authorization and use SMTPS on port 587 with TLS encryption, on the computer that has mailx installed, add the following to the /etc/mail.rc file.

account default {
set smtp=smtp://
set from=""

set smtp-auth=login
set smtp-auth-user=root
set smtp-auth-password="roots cleartext password from the Postfix server"

#Encryption (ssl/tls)
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb

More details at


Create a new file on the client machine named CAfile.pem that contains the public certificate from the Postfix email server. Using an editor, such as vi or nano, edit the CAfile.pem file so that only the certificate is listed. The file should begin with BEGIN CERTIFICATE and end with END CERTIFICATE.

[root@server1 ~]# cd /etc/pki/tls
[root@server1 ~]# openssl s_client -showcerts -starttls smtp -connect > CAfile.pem


Notice that the nss-config-dir is /etc/pki/nssdb. Nssdb stands for Network Security Services Database. By default, the nssdb should not have any Certificate nicknames and list 3 trust attributes.

[root@server1 ~]# certutil -L -d /etc/pki/nssdb

Certificate Nickname                        Trust Attributes


Copy the certificate into the nssdb. 

[root@server1 ~]# certutil -A -n "" -t "TC,," -d /etc/pki/nssdb -i /etc/pki/tls/CAfile.pem


Let's check the nssdb again. Notice there is a new entry.

[root@server1 ~]# certutil -L -d /etc/pki/nssdb

Certificate Nickname                        Trust Attributes
                                            SSL,S/MIME,JAR/XPI                        CT,,


Ensure the certificate is valid.

[root@server1 ~]# certutil -V -n "" -d /etc/pki/nssdb -u A
certutil: certificate is valid


If you need to delete a certificate from nssdb, you can use the -D option.

[root@server1 ~]# certutil -D -n "" -d /etc/pki/nssdb


Send an email.

[root@server1 ~]# mailx -A default
Subject: Example subject
Hello John



The -v (verbose) option can be used to diplay more information.

[root@server1 ~]# mailx -v -A default
Subject: Example subject
Hello John
Resolving host . . . done.
Connecting to . . . connected.
220 ESMTP Postfix
>>> EHLO
250-SIZE 10240000
250 DSN
220 2.0.0 Ready to start TLS
Comparing common name: ""
host certificate does not match ""
SSL parameters: cipher=missing, keysize=256, secretkeysize=256,,,OU=example,O=example,L=City,ST=State,C=US,,OU=example,O=example,L=City,ST=State,C=US
250-SIZE 10240000
250 DSN
235 2.7.0 Authentication successful
>>> MAIL FROM:<>
250 2.1.5 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as A96586012F
>>> QUIT
221 2.0.0 Bye


If problems persist, you can try to specify the ssl method in /etc/mail.rc.

set ssl-method=ssl2
set ssl-method=ssl3
set ssl-method=tls1


Mail log

View the mailog to ensure the email was delivered.

[root@server1 ~]# tail /var/log/maillog
. . . 
Mar 27 21:23:16 mail1 postfix/local[2079]: 94D795845: to=<user1@localhost>, orig_to=<user1@localhost>, relay=local, delay=0.05, delays=0.03/0.01/0/0, dsn=2.0.0, status=sent (delivered to Maildir)
. . .


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.