Linux provides a utility to manage our emails from the command line itself. The mail command is a Linux tool, that allows a user to send emails via a command-line interface.
To take advantage of this command, we need to install a package named
'mailutils'. It can be done by:
sudo apt install mailutils
One thing that must be kept in mind is that,
'mailutils' allows us to connect to a local SMTP (Simple Mail Transfer Protocol) server. Therefore, we can not expect sending mails to an external domain like
Let us now see how to write and send mails through the terminal.
Mail Command syntax
There can be multiple ways to send a simple mail using
Simple Mail Body
mail -s "<Subject>" <receiver@domain> Cc: <Mail Body> <Mail Body> <CTRL + D>
In the above example, the mail is sent from root to the user named,
'aprataksh'. We will look at how to check our mailboxes later in the tutorial.
'-s' is used to denote the “Subject” of the mail.
It must be noted that, to end the body of the mail, the user must interrupt using
'CTRL + D'.
'Cc:' refers to as a carbon copy and is used in case we wish for other parties to get informed of the mail as a reference.
Passing body using ‘<<<‘
mail -s "<Subject>" <receiver@domain> <<< "<Mail Body>"
In the above syntax, we are creating a email using the
Using ‘echo’ command
echo "<Mail Body" | mail -s "<Subject>" <receiver@domain>
The echo command throws the string out. The use of pipe
'|' is to pass the thrown string to the email created later.
Mail body from a file
In case we are weary of writing large messages for email on the command line, we can define the mail body as file and pass it to the
'mail' command by:
mail -s "<Subject>" <receiver@domain> < <FILENAME>
After looking at all the ways to send a generic mail, we will see other provisions of the
Multiple receivers for a single mail
In case, we wish to send the same email to multiple users, we can do so by:
mail -s "<Subject>" "<receiver1@domain>,<receiver2@domain> <<< "<Mail-body>"
Attachments to the mail
Like a normal Email application,
mail -s "<Subject>" -A <PATH>/<FILENAME> <receiver@domain> <<< "<Mail body>"
'--attach' options are used to attach files. In the above example, the file — ‘my_program.cpp’ has been attached.
Cc and Bcc
A regular Email user would know the benefits of Carbon copy and Blind carbon copy, with respect to emails. To make use of this utility, we can use
'-c'option for Cc, whereas
'-b' option for Bcc.
mail -s "<Subject>" -c "<receiver@domain>" -b "<receiver@domain>" <receiver@domain> <<< "<Mail-body>"
There is no hassle to check the mailbox. All we need to type is
'mail' and press
'ENTER'. Since, we were sending to the user
'aprataksh', we will check it that particular mailbox.
Access an email
To open the mails, we need to mention the specific mail number associated to it. For instance, let us read the first mail.
The mail system provides all sorts of information related to the mail.
Navigate through the emails
In order to open the next email, we can enter
'+' in the prompt, whereas,
'-' is used to access the previous mail in the mailbox.
Deleting unimportant mails
It is fairly easy to delete mails from the mailbox. All we have to do is open a mail using the number and press
There are techniques to delete multiple emails at once:
- To delete emails numbered 6 and 9 –
'd 6 9'
- To delete from 4 to 40 –
- To delete all the mails –
Extract the attachments
After sending a attached file through the
To answer the question, first we need to understand how the attachments are sent. The attachments go through base64 encoding. It basically converts them into a text, that can be decoded on the other side.
In the example for attachment we sent
'my_program.cpp' file. Let us look at it on the other side.
The new task at hand is to decode the encoded text. To achieve this, we first need to save the encoded data in a file. Then we use the base64 command:
base64 -d encoded-data-file
This is pretty much everything, for the functioning of the mailbox.
Quit the mailbox
After reading and deleting the mails, we can quit in the following ways:
'CTRL + D'– If we quit using this method, all the emails we have already read will be saved and transferred to the local mailbox, stored in
As we can see, we accessed only one email, therefore the seen message is transferred to the local mailbox. The unread emails are not touched.
'xit'– If we quit the mailbox using the
'exit'command, no changes are made to the mailbox contents.
'CTRL + Z'– This is a common tool for stopping a running process. Similar to
'exit'command, no changes are made to the current mailbox.
Access other mailboxes
As we told earlier, after reading the mails,
mail -f <PATH>/<FILENAME>
It is really easy to use