Create and read emails using the MAILX command in Linux

Home > Search > Linux commands
  by

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 john.doe@example.com. This assumes there is a user account for john.doe on the example.com email server. In this example, after typing "Hello John", press Ctrl + D to send the email.

[root@server1 ~]# mailx john.doe@example.com
Subject: Example subject
Hello John
EOT

 


Subject

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" john.doe@example.com

 


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 john.doe@example.com

 


Carbon copy

The -c option can be used to carbon copy.

[root@server1 ~]# mailx -c jane.doe@example.com john.doe@example.com

 

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

[root@server1 ~]# mailx -b jane.doe@example.com john.doe@example.com

 


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://mail.example.com:587
set from="no-reply@example.com"

#Authentication
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 http://heirloom.sourceforge.net/mailx/mailx.1.html.

 

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 mail.example.com:587 > 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
                                            SSL,S/MIME,JAR/XPI

 

Copy the certificate into the nssdb. 

[root@server1 ~]# certutil -A -n "mail.example.com.crt" -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

mail.example.com.crt                        CT,,

 

Ensure the certificate is valid.

[root@server1 ~]# certutil -V -n "mail.example.com.crt" -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 "mail.example.com.crt" -d /etc/pki/nssdb

 

Send an email.

[root@server1 ~]# mailx -A default user1@example.com
Subject: Example subject
Hello John
EOT

 


Verbose

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

[root@server1 ~]# mailx -v -A default user1@example.com
Subject: Example subject
Hello John
Resolving host mail.example.com . . . done.
Connecting to 192.168.0.10:587 . . . connected.
220 mail.example.com ESMTP Postfix
>>> EHLO client.example.com
250-mail.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITTIME
250 DSN
>>> STARTTLS
220 2.0.0 Ready to start TLS
Comparing common name: "mail.example.com"
host certificate does not match "mail.example.com"
SSL parameters: cipher=missing, keysize=256, secretkeysize=256,
issuer=E=admin@example.com,CN=mail.example.com,OU=example,O=example,L=City,ST=State,C=US
subject=E=admin@example.com,CN=mail.example.com,OU=example,O=example,L=City,ST=State,C=US
>>>EHLO client.example.com
250-mail.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ESTN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITTIME
250 DSN
235 2.7.0 Authentication successful
>>> MAIL FROM:<user1@example.com>
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.




Comments