Install mail Command

Run the following command to install mail

sudo apt install mailutils

When prompted to configure the postfix package, press TAB to select Ok and confirm with Enter.

Postfix Configuration

Select Internet Site in the package configuration window. Press TAB to select Ok and press Enter to confirm.

Postfix Configuration

Complete the basic configuration by entering the system mail name (the machine's fully qualified domain name).

Postfix Configuration
mail Command Options
Argument Description Example
-s Specifies the subject of the email. mail -s 'Subject' user@example.com
-c Sends a carbon copy of the email to the specified address. echo 'Hello' | mail -s 'Subject' -c cc@example.com user@example.com
-b Sends a blind carbon copy of the email to the specified address. echo 'Hello' | mail -s 'Subject' -b bcc@example.com user@example.com
-a Adds an attachment to the email. echo 'Hello' | mail -s 'Subject' -a file.txt user@example.com
-q Reads a message from the specified file. mail -s 'Subject' -q file.txt user@example.com
-r Specifies the return address for the email. echo 'Hello' | mail -s 'Subject' -r from@example.com user@example.com
-v Verbose mode. Displays the details of the email sending process. echo 'Hello' | mail -s 'Subject' -v user@example.com
-f Reads messages from the specified mailbox file. mail -f /var/mail/user
-i Ignores lines with only a dot. mail -i -s 'Subject' user@example.com
-E Does not send messages with an empty body. echo '' | mail -E -s 'Subject' user@example.com
mail Command Examples
Example 1: Send Mail with Subject and Body

The basic way of using mail to send emails is to specify the email subject using the -s flag and add the recipient. The command then enters the interactive mode, asking if you want to send Ccs and asking for the email body.

After entering all the information, press Ctrl+D to send the email.

[satella@ubuntu ~]$ mail -s "Email Subject" echidna
Cc:
This is the email body
Example 2: Send Mail Using echo

Use the echo command to send an email without entering the interactive mode of the mail command. Write the email body and pipe the echo command output to the mail command.

[satella@ubuntu ~]$ echo "This is the email body" | mail -s "Email Subject" echidna
Example 3: Send Email Using Redirection

Another way to provide the email body is to use redirection (<<<). Using redirection also avoids the mail command interactive mode and sends the email without any prompts.

[satella@ubuntu ~]$ mail -s "Email Subject" echidna <<< "this is the email body"
Example 4: Send Email from File

The mail command allows users to provide the email body from an existing file. This option saves time and facilitates sending large messages without writing them in the command line.

[satella@ubuntu ~]$ echo "This is the email body" > file.txt
[satella@ubuntu ~]$ mail -s "Email Subject" echidna < file.txt
Example 4: Read Mail

Check the mailbox for the current user by running the mail command without any options or flags.

[minerva@ubuntu ~]$ mail
"/var/mail/minerva": 8 messages 8 new
U   1 satella@ubuntu Thu Jun 13 20:52  17/532   this is email subject
U   2 satella@ubuntu Thu Jun 13 20:54  16/488   this is email subject
U   3 satella@ubuntu Thu Jun 13 20:58  16/488   this is email subject
U   4 satella@ubuntu Thu Jun 13 20:59  16/488   this is email subject
U   5 satella@ubuntu Thu Jun 13 21:02  16/488   this is email subject
U   6 satella@ubuntu Thu Jun 13 21:05  16/492   this is email subject 1
U   7 satella@ubuntu Thu Jun 13 21:06  16/492   this is email subject 1
U   8 satella@ubuntu Thu Jun 13 22:07  16/416   Subject
?

To open an email, enter the email number and press Enter. For example, we will open email number 8:

? 8
Return-Path: <satella@ubuntu>
X-Original-To: minerva
Delivered-To: minerva@ubuntu
Received: by ubuntu (Postfix, from userid 1001)
	id 453F4A257B; Thu, 13 Jun 2024 22:07:00 +0700 (WIB)
Subject: Subject
To: minerva@ubuntu
User-Agent: mail (GNU Mailutils 3.14)
Date: Thu, 13 Jun 2024 22:07:00 +0700
Message-Id: <20240613150700.453F4A257B@ubuntu>
From: satella@ubuntu
X-UID: 8
Status: O

?

After opening the email, the command waits for further input from the user. Open the next email by entering (+) in the prompt, or access the previous email by entering (-). Return to the mailbox by pressing Ctrl+D.

Example 5: Delete Mail

To delete an email, open the email you want to delete, press d, and confirm with Enter. To delete multiple emails at once, open the mailbox and enter d followed by the email numbers you want to delete.

For example, the following command deletes emails numbered 1 and 2:

d 1 2

To delete a range of emails, specify the first and last email in the range separated by a dash. For example, to delete emails 1-10, open the mailbox and run:

d 1-10

To delete all received mail, open the mailbox and run:

d*