mailx command in linux – send and receive mail

Mailx is an character-based, intelligent mail processing system in Linux 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

You can easily install mailx using steps mentioned in this article if it is not there.

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 the 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 in learning more about this utility, visit mailutilis documentation page or run the man mailx or mailx --help command on terminal

Using mailx command

You can use mailx command to read, write, reply, and delete messages. It has a host 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 Linux Distro.

1. Reading emails/messages using mailx

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

mailx output when run with any parameter

First, you get a summary like a User Mailbox, Total Messages, Total new and unread emails, 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, and other users’ 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.

Read email using mailx

Just type the sequence of the email message e.g 1, 2 on the terminal to read the respective email. 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

2. Replying to a mail/message using mailx

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


using mailx to reply message

3. Deleting email message using mailx

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 or multiple messages or you can even specify the range of the message.

a) Delete message 1

d 1

This deletes the message with sequence number 1.

b) Delete multiple messages e.g. 1 and 2

d 1 2

c) 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 the exit option, as mentioned below nothing happens on the message as mailx discards all the changes are done on the mailbox.

4. Quit/Exit mailx command

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

What is difference between quit and exit?

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

5. Sending email using mailx

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

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

a. Send mail with subject and body

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

It displays EOT 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

b. Send mail with no message body

The below example sends an email with no message body.

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

c. 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 email the log file.

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

d. Send mail with a file as an attachment

Below command sends a file as an attachment.

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

e. Send mail with CC and BCC

Use the CC and BCC option to send emails to the CC and BCC addresses.

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


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

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

Scroll to Top