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 [-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 mailx utility, visit mailutilis documentation page or run the man command as shown below.
$ man mailx
Just use the –help option to quickly get help.
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 18.04 and Ubuntu 20.04 Linux Distro.
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.
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
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.
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.
Replying to a mail/message
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.
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.
Delete message 1
This deletes the message with sequence number 1.
Delete multiple messages e.g. 1 and 2
d 1 2
Delete multiple messages by specifying the range.
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.
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 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.
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.
1. 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] Hi How are you? This is a test email. EOT
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.
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 email the log file.
mailx -s "Test Email" [email protected] < filename
4. Send mail with a file as an attachment
Below command sends a file as an attachment.
mailx -s "Test Email" -a filename [email protected]
5. 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.