1. Overview

The mail command is a Linux utility that enables users to send and receive emails via the command line. Additionally, the tool is useful for systems without a GUI (Graphical User Interface) environment or for sending automated emails using shell scripts.

In this tutorial, we’ll learn how to efficiently use the mail command to send and receive email directly from the terminal. Further, we’ll also look at different options used with the mail command. Finally, we’ll explore various mail command examples.

2. Install mail Command

Before we use the mail command, it’s essential to ensure that the necessary package, mailutils, is installed on the system. mailutils provides the fundamental toolset required for sending and receiving emails via the command line in a Linux environment. Of course, it also includes the mail command itself.

Thus, let’s proceed with installing the mailutils with the apt command:

$ sudo apt install mailutils

The mailutils toolkit can connect to any SMTP (Simple Mail Transfer Protocol) server and can function as a full-fledged mail sender if needed. However, it’s important to note that certain configurations may be required to enable communication with external SMTP domains.

3. Common mail Command Options

The basic syntax of the mail command is fairly straightforward:

$ mail [ options ] -s [ subject ] [recipient_address]

Let’s see the meanings of each of the components of the mail command:

  • [options]: represents various flags and parameters to customize the email and the behavior of mail
  • [subject]: denotes the subject of the email
  • [recipient_address]: specifies the recipient email address

Now, let’s explore common options available with the mail command:

Options Description
-d Outputs various information useful for debugging
-s “subject” Specifies the email subject on the command line
-n Inhibits reading /etc/mail.rc on startup
-N Prevents initial message headers when reading mail or editing a mail directory
-i Causes mail to ignore tty interrupt signals
-A [file_path] Attaches a file to the email
-E Prevents sending messages with an empty body
-a=[header] Enables users to append a specified message to the email
-I Forces the interactive mode, even when input is not a terminal

With these various options, we can customize the email sending and receiving experience to suit specific needs.

4. Common mail Command Examples

Let’s dive into practical examples of using the mail command with various options. In particular, for these examples, we use root and kolawole users as the recipients created in the Linux system.

4.1. Sending Mail in Interactive Mode

To initiate a straightforward email without any attachments or additional customization, we use the mail command with the -s option to just specify the subject.

For example, let’s send a basic email to the root user:

$ mail -s "hello" root
Cc:
hello, welcome to kolawole world

In this command, we send a small email. We’re then prompted to input the optional Cc field. We press Return and type the body of the email as hello, welcome to kolawole world. Finally, we press Return and then Ctrl-D to send the mail.

This is a quick and simple way to start communication directly from the command line.

4.2. Sending Mail Using echo

Alternatively, we can use the echo command to send an email without entering the interactive mode of the mail command like we did above.

For example, let’s send an email with the use of the echo command:

$ echo "hello, welcome to kolawole world" | mail -s "hello" root

In this example, we pipe the output of echo, which is “hello, welcome to kolawole world”, to the mail command. Further, we also specify the subject of the email as hello.

This method is useful for sending quick, one-line messages without entering the interactive mode.

4.3. Sending Mail Body From File

In scenarios where the content of the email body is lengthy or stored in a file, we leverage the mail command to send the email body directly from that file:

$ mail -s "Meeting Minutes" kolawole < meeting_minutes.txt

In this example, we send an email with the subject “Meeting Minutes” to the user kolawole, and the body of the email is sourced from the file named meeting_minutes.txt.

4.4. Sending Mail With Attachments

Including attachments in emails is often necessary for sharing documents, images, or other files. Furthermore, the mail command supports this functionality using the -A option followed by the path to the file we want to attach:

$ mail -s "Monthly Report" -A monthly_report.pdf kolawole < report_body.txt

In the command, we’ve attached a file named monthly_report.pdf to the email with the subject “Monthly Report” and sent it to the user kolawole.

Additionally, we provide the content of the email body from the file report_body.txt. This enables us to send comprehensive emails with attachments directly from the command line.

4.5. Sending Mail to Multiple Users

In some situations, we may need to send email to multiple users. The mail command can be used to achieve this by specifying multiple email addresses separated by commas.

For example, let’s use the mail command to send email to several users:

$ echo "Reminder: Team Meeting Tomorrow at 10 AM" | mail -s "Meeting Reminder" kolawole, root

In this command, we use the echo command to create the email body, reminding recipients about the upcoming team meeting. We then specify the subject as “Meeting Reminder” and provide a list of email addresses separated by commas.

4.6. Incorporate mail in Scripts

In addition to the manual use of mail, the command can also be incorporated into shell scripts to automate tasks.

For example, let’s create a Bash script that reports disk usage via email.

Firstly, we create a script named disk_report.sh using a text editor and add the following lines of command:

$ cat disk_report.sh
#!/bin/bash
du -sh | mail -s "Disk usage report" kolawole

Then, we save the file and run the script via Bash:

$ bash disk_report.sh

The script executes and sends an email to the user kolawole with the du command output.

5. Conclusion

In this article, we explored the versatility of the mail command, discovering its usefulness for sending and receiving emails, as well as automating email tasks directly from the command line.

Additionally, understanding its options and examples enables users to effectively handle email communication in Linux environments, enhancing productivity without depending on graphical interfaces.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments