mailx command in linux – send and receive mail

mailx command in linux

Mailx is an character-based, intelligent mail processing system. You can use it to send and receive emails.  This is based on Berkeley Mail 8.1 and provides the complete functionality of the POSIX mailx command. Extensions for MIME, IMAP, POP3, SMTP, and S/MIME are also available.

Features of mailx,

  • Enhanced features for interactive use
  • Caching and disconnected operation for IMAP
  • Message threading
  • Scoring, and filtering

mailx should not be availalble on your system unless you install it. You can use this article which should help you to install mailx.

Here I am going to cover mailx. We will see how to read, send and delete emails using the mailx unility.

mailx syntax

mailx [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr


  • -s – subject of an email. Mention it in double-quotes if it contains space
  • -a – attach the given filename to message
  • -b – send blind carbon copies to list of users
  • -c – send carbon copies to list of users
  • -r – set the from address.
  • -v – Verbose mode. This displays details of delivery on the user’s terminal

Note:- All parameters to this command are optional.

Those who are interested to learn more about mailx utility, visit mailutilis documentation page or run man command as shown below.

$ man mailx

Just use –help option to quickly get help.

mailx --help

Using mailx command

You can use mailx command to read, write, reply, delete messages. It has hosts of features even though it is command-based. We are going to cover the basics of mailx for practical purposes. Covering all aspects of this command is beyond the scope of this article.

All the examples mentioned below are tested with GNU mailx on Ubuntu 18.04 and Ubuntu 20.04 Linux Distro.

Reading emails/messages

mailx command by default opens the current user’s system mailbox (/var/mail/<user>), so just Open the Unix/Linux terminal and type mailxwithout any parameters on the command prompt to read emails.

mailx output when run with any parameter

First, you get a summary like a User Mailbox, Total Messages, Total new and unread email, etc. There is a list of all emails with the sequence number, status, date received, size and subject.

mailx with -f option allows you to open your personal mailbox, other user’s system mailbox if you have access as shown below.

mailx -f /home/tom/mbox

mailx -f to open personal mail box

Now you are inside your mailbox. Just type the sequence of the email message e.g 1, 2 on the terminal to read the respective email.

Read email using mailx

As soon as you read emails, the status of the message changes from New to Read. Type h on the command to list all the headers of the message.

mailx he option to display email headers

Replying to a mail/message

Use the r option to reply to the email. Just type r with message number to reply to that message. If you omit the number, it will reply to the current message.


using mailx to reply message

Deleting message

Use the d option followed by the number of messages to delete the message. Check the below example illustration. mailx allows you to delete one message, multiple messages or you can even specify the range of the message.

Delete message 1

d 1

This deletes the message with sequence number 1.

Delete multiple message e.g 1 and 2

d 1 2

Delete multiple messages by specifying the range.

d 1-5

Changed your mind and now want to recover the message, just use u option to undelete the deleted message. Specify the number of the message with this option.

Remember, once you delete the message and exit the mailbox with the quit command. The message gets permanently deleted from your mailbox. If you use exit option, as mentioned below nothing happens on the message as mailx discard all the changes are done on the mailbox.

Quit/Exit mailx command

You can exit mailx command using either the quit or exit option.

quit vs exit

The quit option remembers message status and moves read emails to mbox folder, permanently deletes deleted message while the same is not the case with the exit. It just exits the command without saving any changes done to the mailbox.

Sending email

The mailx command supports good options which help you to send an email with and without attachment. It supports cc and bcc options as well.

Now, let’s explore some practical options of mailx command with examples.

1. Send mail with subject and body

Just log in to the Unix console and run the below command and hit enter. Then enter the message body and click on Ctrl+D to complete the message.

It displays EOT and exists the message body and sends the mail.

mailx -s "Test Email" [email protected]
How are you?
This is a test email.

You can also use ‘|’ pipe to pass the message body to mailx command as shown below.

 echo "Hi How are you" | mailx -s "Test Email" [email protected]

Below is the email received.

mailx sample received email

2. Send mail with no message body

The below example will send an email with no message body.

mailx -s "Test Email" [email protected] < /dev/null

3. Send mail with file content as a message body

You can use the below command to send the content of the file as the message body. Generally, this method is used to an email the log file.

mailx -s "Test Email" [email protected] < filename

4. Send mail with a file as an attachment

Below command send a file as an attachment.

mailx -s "Test Email" -a filename [email protected]

5. Send mail with CC and BCC

Use CC and BCC option to send email to CC and BCC address.

mailx -s "Test Email" -c [email protected] -b [email protected] [email protected]


As I said about, mailx provides complete functionality for email management in Unix and Linux operating system, even it is character-based.

We covered basic aspects of this command and I would recommend visiting mailutils for further information or run man mailx command on the terminal to get complete details.


Share This:
Scroll to Top