Linux

How to send emails from linux terminal or script using mail command

Pinterest LinkedIn Tumblr

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

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:

mail

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

Usage

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

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.

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

Use the 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

Send an email from [email protected] to [email protected] 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 @hostname.

mail -s "Test Subject" username 

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:

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.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment