The mail
command is a command-line utility that allows us to send emails from the command line. This is useful especially if you want to script the process of sending emails. You can either type the command in the terminal passing it arguments to send the email or you can create a script that sends the email as part of the process either attaching files or looping some data and processing while sending the mail.
There are other terminal based command line clients that can also be used to send emails like sendmail, mutt, SSMTP, telnet but in this guide we will focus on the mail
command.
The mail command invokes the standard sendmail binary (/usr/sbin/sendmail) which in turns connects to the local MTA to send the mail to its destination. The local MTA is a locally running smtp server that accepts mails on port 25.
This means that an smtp server like Postfix should be running on the machine where you intend to use the mail command. If none is running you get the error message “send-mail: Cannot open mail:25”.
Installation
Install with this command:
For Ubuntu
|
|
For centos 7
|
|
For Alma/Rocky Linux/Rhel
|
|
Test that the command is successfully installed with this command:
|
|
The above command checks if there is an email for the current user. You should see an output like this:
|
|
Usage
Sending simple email
Use this command to send an email to johndoe@example.com
. The -s
command line argument is for specifying the subject.
|
|
The above command will block upon hitting enter waiting for you to type your message. When you are done use Ctrl-D
to notify the mail client that you are done and the email is ready to send.
|
|
The EOT
was entered when I pressed Ctrl-D
. Once you do that, the mail command would dispatch the message for delivery and done.
Send message with content from a file
Use this command to send a message from the file /path/to/file.txt
.
|
|
You can also use this one liner where you echo out the content to send then pipe it to the mail command.
|
|
Send an email to multiple recipients
To send an email to multiple recipients, you can use a comma separated list of emails like in this example:
|
|
Adding CC and BCC recipients
Blind carbon copy (BCC) and carbon copy (CC) address can also be attached. Use -c <email>
for CC and -b <email>
for bcc.
This example specifies CC and BCC:
|
|
Specify From name and address
You can use option -r
to specify the from name and address as seen below:
|
|
Alternatively, you can use -a
to append an additional header to attach with the message. Here is an example of how to provide FROM
as part of that.
|
|
To specify the from name, use the following syntax:
|
|
Please note that we have to escape the less and great arrows since they have special meaning for the shell prompt. When you are issuing the command from within some script, you would omit that.
Specify “Reply-To” address
Use the replyto
to specify reply to address like in this example:
|
|
Email with an attachment
Send an email from johndoe@example.com
to janedoe@example.com
with an attachment.
|
|
Email to a local system user
To send an email to a local system user, only specify the username in place of recepient. You can append the hostname but its not necessary using @hostname
.
|
|
Enabling Verbose
If you need to check what is going one while the email is being processed, enable verbose output with the arg -v
. This is good for troubleshooting like when you want to check the SMTP commands being used by the mail command . Here is an example:
|
|
That is it!
In this guide, we learnt how to install and use the mail command in linux.