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
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”.
Install with this command:
sudo apt-get install mailutils
For centos 7
sudo yum install -y mailx
For Alma/Rocky Linux/Rhel
sudo dnf install -y mailx
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:
➜ mail No mail for centos
Sending simple email
Use this command to send an email to
[email protected]. The
-s command line argument is for specifying the subject.
mail -s "Test mail" [email protected]
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.
❯ mail -s "Test mail" [email protected] Hey John, Please remember to send me the Linux Book. Regards, Me. EOT
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
mail -s "Test mail" [email protected] < /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.
echo "Hello world" | mail -s "Test mail" [email protected]
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:
echo "Hello world" | mail -s "Test mail" [email protected],[email protected]
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:
echo "Hello world" | mail -s "Test mail" -c [email protected] -b [email protected]
Specify From name and address
You can use option
-r to specify the from name and address as seen below:
echo "Hello world" | mail -s "Test mail" –r "Kip<[email protected]>" [email protected]
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.
echo "Hello world" | mail -s "Test mail" [email protected] -aFrom:[email protected]
To specify the from name, use the following syntax:
echo "Hello world" | mail -s "Test mail" [email protected] --aFrom:Kip\<[email protected]\>
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
replyto to specify reply to address like in this example:
echo "Hello world" | mail -s "Test mail" [email protected] [email protected]
Email with an attachment
mail -s "The File you requested" -a ~/Documents/file.txt -r [email protected] [email protected] <<EOF Hi jane, Please find the file requested attached. Kind Regards, John. EOF
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
mail -s "Test Subject" username
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:
mail -v -s "Hello theere" [email protected] <<< 'This is the message'
That is it!
In this guide, we learnt how to install and use the mail command in linux.