Javatpoint Logo
Javatpoint Logo

Linux mail Command

Linux mail command is a command-line utility that allows us to send emails from the command line. It will be quite useful to send emails from the command line if we want to generate emails programmatically from shell scripts or web applications. The mail command can be used directly by the terminal as well as the Shell script. However, many other commands are available in Linux that can be used to send the emails from the command line such as sendmail, mutt, SSMTP, telnet, and more. In this section, we will focus on the mail command.

Installation of mail command in Linux

The mail command can be installed by various packages such as:

  • gnu mailutils
  • heirloom-mailx
  • bsd-mailx

Each package facilitates with different features and options. For example, the heirloom-matrix package is capable of using an external smtp server for sending messages, while the rest two servers use the local smtp server.

We will install the mailutils package, the most popular way to send emails from the command line. It supports the mail command to send the emails.

To install the mailutils package, execute the below command:

The above command will ask for a system password, type the password, and press ENTER key. It will verify the installation, type 'y' to confirm the installation. Consider the below snap of output:

Linux mail Command

It will start a daemon process and ask for the postfix configuration, select your desired postfix option.

Linux mail Command

There four postfix options are available for installation. They are as follows:

Linux mail Command

To select your desired option, scroll the option by using ARROW keys, and select it by using TAB+ ENTER keys. Postfix interacts with the server to process requests. After selecting the postfix option, it will successfully install the mailutils package on your machine.

How does the mail command work?

It is important to know how the command is working. The mail command of the mailutils package invokes the standard sendmail binary to send the mail to a specified destination. It connects to the local MTA, which is a local running SMTP server that supports mails on port 25.

It means that a postfix that contains an smtp server should be running on our machine for using the mail command. The postfix asks DNS, the destination where to deliver the mail, DNS replies a list of mail servers and creates an SMTP connection. If it is missing, we will get the error message like "send-mail: Cannot open mail:25".

Send mail using the mail command

Sending mails from the command line is a straight forward process. To send a mail from the terminal, execute the command as follows:

The '-s' option is used to specify the subject. After executing the command, it will ask for the Cc address. Type the Cc (carbon copy) address; you can leave it blank by hitting Enter key. Type the text, and press CTRL+D keys to send it. Consider the below output:

Linux mail Command

From the above output, the mail will be sent to a specified mail address.

Specify the mail body in a single line

We can specify the subject and message in a single line. To specify the message body in a single line, execute the below command:

The above command will send the mail to the specified address. Consider the below command:

Linux mail Command

Or we can also specify the command as follows:

Fetch message from a file

We can also fetch messages from a file. It is useful if we want to call the mail command from a shell script or other programs. To send a message from a file, execute the command as follows:

The above command will send the message from the file mailcontent.txt. For quick, we can also use the below command:

Specify CC and BCC

We can also attach a bcc and cc address within a command. To attach a bcc and cc address, use the -b and -c options, respectively.

To add a bcc address, execute the command as follows:

To add a cc address, execute the command as follows:

Also, we can specify both cc and bcc addresses in a single command. Execute the command as follows:

Adding multiple recipients

The mail command allows us to add more than one recipient in a single mail other than cc and bcc. To add more than one mail address, just put the other mail addresses after the first mail address followed by a comma(,). Consider the below command:

The above command will deliver the message to both addresses.

Specify the sender name and address

We can add the additional header information such as 'FROM' name and address to attach with the email. To specify the additional information with the mail command, use the -a option with the command. Execute the command as follows:

The above command will deliver the specified information to the receiver.

Adding an attachment

We can also attach a file with the mail. The -a option is used to attach a file along with the mail. To add an attachment, execute the command as follows:

The above command will attach a specified file from the specified path with mail.

Read mails

The mail command can be used to read all your incoming mails. However, it is not cool to read mails from the command line, but we can understand it for the shake of knowledge. To read the inbox mails, execute the mail command without any option as follows:

The above command will display all incoming mails.

Manual for the mail command

To read the manual for the mail command, execute the man command as follows:

The above command will display the manual of the mail command on your terminal. Consider the below output:

Linux mail Command

Press the 'q' key to exit from the manual and 'h' key for help. The help option lists more options to assist you. Consider the below image:

Linux mail Command

To exit from the help, press the 'q' key.


Next TopicLinux traceroute





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA