The mail
command in Linux allows users to send and receive email from the command line. It is a simple and efficient way to send messages and attachments without the need for a graphical user interface. It is commonly used in shell scripts to automate email notifications and alerts.
Overview
The mail
command can be used in two modes: interactive and non-interactive. In interactive mode, users can compose and send emails from the command line. In non-interactive mode, users can send emails using a pre-written message and subject.
To start the interactive mode of the mail
command, simply type mail
in the terminal. This will open the mail prompt, where users can compose and send emails. The basic syntax for sending an email is as follows:
mail -s "Subject" recipient@example.com
This will open the mail prompt and allow users to compose the email. Once the message is complete, press Ctrl+D
to send the email.
In non-interactive mode, users can send emails using a pre-written message and subject. The basic syntax for sending an email in non-interactive mode is as follows:
echo "Message" | mail -s "Subject" recipient@example.com
This will send an email with the specified message and subject to the recipient’s email address.
Options
The mail
command has several options that can be used to customize its behavior. The following table lists all available options:
Option | Description |
---|---|
-s | Specifies the subject of the email |
-a | Attaches a file to the email |
-c | Sends a copy of the email to the specified recipient(s) |
-b | Sends a blind copy of the email to the specified recipient(s) |
-r | Specifies the sender’s email address |
-q | Quits the mail prompt without sending the email |
Troubleshooting tips
- If the
mail
command is not working, make sure that thesendmail
orpostfix
service is running on the system. - If the email is not being delivered, check the spam or junk folder of the recipient’s email account.
- If the email is being rejected by the recipient’s email server, check if the sender’s email address is on a blacklist.
Notes
- The
mail
command is not a full-featured email client and does not support advanced features such as HTML formatting or encryption. - The
mail
command can be combined with other Linux commands to automate email notifications and alerts.