Linux mail command examples – send mails from command line

59 Flares Filament.io 59 Flares ×

Send and receive mails from command line

For sending and receiving mails we are used to web based services like gmail, yahoo etc or desktop based mail clients. However on linux we can easily send/receive mails from command line as well. This is useful in situations like sending mails from shell scripts or through some other kind of program.

This tutorial explains, how to use to the mail command on linux to send and receive mails.

Send mails

Depending on your linux distribution, you may need to install additional packages like mailutils to get the mail command to work.

Debian/Ubuntu users can use the apt-get command to install it

# apt-get install mailutils

Fedora / CentOS / Red Hat Enterprise Linux (RHEL) users can use yum

# yum install mailutils

Now you should have the mail command ready to work.

Run the command below, replacing "user@yourmaildomain.com" with your e-mail address. The s option specifies the subject of the mail followed by the recipient email address.

$ mail -s "Hello World" user@yourmaildomain.com

Now you have to type in your message. When you're done, hit ‘control-D’ at the beginning of a line

# mail -s "This is the subject" mail@example.com
Cc: 
Hi Silver
How are you doing
Hope its going fine.
Bye

Now, the first mail is sent on command line. The content of the mail can be taken from a file as follows:

In some distributions, the shell asks other fields interactively like 'Cc' (Carbon copy) first. Just hit enter here to skip. Next type in your message. When you enter the mail address or addresses with commas, you need to press enter to go to the new line for message body.

After hitting Ctrl+D the shell will return to command prompt without any further message which you might be expecting. However our mail would be send out.

It is interesting to see that if you do not enter a message body and hit Ctrl+D, the mail command would react.

# mail -s "This is the subject" mail@example.com
Cc: 
Null message body; hope that's ok

If the mail body is in a file then we can use it directly to send the mail. This is useful when calling the mail command from shell scripts or other programs written in perl or php for example.

$ mail -s "Hello World" user@yourmaildomain.com < /home/user/mailcontent.txt

Or a quick one liner

# echo "This is the message body" | mail -s "This is the subject" mail@example.com






Other useful parameters in the mail command are:

-s subject (The subject of the mail)
-c email-address (CC - send a carbon copy to email-address)
-b email-address (BCC - send a blind carbon copy to email-address)

Here's and example of how you might use these options

$ mail -s "Hello World" user@yourmaildomain.com -c usertocc@yourmaildomain.com -b usertobcc@yourmaildomain.com

It is also possible to specify multiple recipients by joining them with a comma.

$ mail -s "Hello World" user1@yourmaildomain.com,user2@yourmaildomain.com

Setting the "FROM" address during sending mails from command line is easy but a little bit tricky.
The trick is : after the mail you want to send to, you add

(double dash) (space) (single dash) (NO space) f

Here's a quick example

mail -s "Hello World" user@yourmaildomain.com -- -f from_user@yourmaildomain.com

However, the above will work on centos, but not on debian or ubuntu. On ubuntu/debian system an alternative syntax has to be used

# echo "This is the message body" | mail -s "This is the subject" mail@example.com -aFrom:blog@binarytides.com

The a option basically adds additional headers. To specify the from name, use the following syntax.

# echo "This is the message body" | mail -s "This is the subject" mail@example.com -aFrom:Blogger\<blog@binarytides.com\>

Note that we have to escape the less/great arrows since they have special meaning for the shell prompt.

Send mail with attachments

The mail command used until now is fast and easy but unfortunately it does not support sending attachments. Now, we move one step further and introduce MUTT. Actually mutt is a very powerful command line mail client and capable of doing more than sending mails with attachments but for now we'll only focus on basic file attachments.

If mutt is not installed on your box, you can use the apt-get or yum commands as a root user (or use sudo instead).

For Debian / Ubuntu Linux users:

# apt-get install mutt

Fedora / CentOS or Red Hat Enterprise Linux (RHEL) users:

# yum install mutt

Now you are ready to send mail with attachments with command line interface.

Send a simple mail

# echo "This is the message body" | mutt -s "hello" mail@example.com

Send mail with attachment

$ mutt -s "Hello World" -a /home/user/file_to_attach.tar.gz user@yourmaildomain.com < home/user/mailcontent.txt

As simple as that.

Send mail with bash/shell scripts

Now, let's dive in more details and try send mails with shell scripts. Here's an easy shell script that reports disc usage

#!/bin/bash
du -sh | mail -s "disk usage report" user@yourmaildomain.com

Open a new file and add the lines above to that file, save it and run on your box. You will receive an e-mail that contains "du -sh" output.

Final trick about mailing from command line is adding attachments to the mail that we send with shell script. Assume that you need to backup a configuration folder, archive it, and send as an attachment with mutt:

#!/bin/bash
tar -zcf /home/user/backup.tar.gz /home/user/files_to_backup
echo "Archived configuration files" | mutt -a /home/user/backup.tar.gz -s "backup data" user@yourmaildomain.com

The echo at the beginning of the third line adds "Archived configuration files" text into the body of the mail.

Read mails

Until now, we covered details about sending mails from command line interface. Actually it is also pretty easy to read mails from command line too:

$ mail

You've probably realized that we're using the same command for sending and receiving mails.
But for reading, no parameters needed.

Here's a sample output

$ mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/hcg": 2 messages 2 new
>N  1 hcg@hcghost.localdomain  Thu Aug  1 16:53  81/3034  "Hello World"
N  2 hcg@hcghost.localdomain  Thu Aug  1 16:54  82/3061  "disk usage report"
&

The first line of the output is the version of the mail program. Second line is a summary saying that there are 2 messages, both of them are unread, and these mail are stored in the /var/spool/mail/hcg file.

All users on a linux system have a mail file in /var/spool/mail/ directory. When a new mail arrives, it is appended to the end of that file. After the file information, the list of mails is displayed.

It's strongly recommended not to edit this mail file without mail or another program. It's fine to read or search with grep or other utility.

To read the first mail, just enter 1 and press enter. While reading the mails, if you want to go back to mail list, just type "q". The > indicates which message is the current one you're working on.

Maildir-utils command

'mu' is a set of command-line tools for Linux/Unix that enable you to quickly find the e-mails you are looking for.

Debian/Ubuntu users can use the apt-get command to install it

# apt-get install maildir-utils

Fedora / CentOS / Red Hat Enterprise Linux (RHEL) users can use yum

# yum install maildir-utils

There are a couple of easy commands in this package like:

$ mu find from:william subject:report

to search mails from william with subject report.

To check the current mail configurations use the info option.

# mu-tool info
VERSION=2.99.97
SYSCONFDIR=/etc
MAILSPOOLDIR=/var/mail/
SCHEME=mbox
LOG_FACILITY=mail
.....

To summarize, sending and reading mails with command line interface is pretty easy and fast when you know how to do it.

Last Updated On : 22nd August 2014

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at m00n.silv3r@gmail.com. Or find him on

  • thedailyexposer.com

    yum install mail

  • Kiran Gaura

    really interesting article. keep up the good work on linux command line. and keep posting the findings here.

  • steve

    I am using Linux Mint 13 (fully updated) installed all the packages listed in the article. Followed the tutorial and the commands work (sorta) but mail is never sent and their is no hcg directory in var/spool/mail , only a locked file called root and when I try to read the mail messages the command “mail” just brings up system log files and displays them as messages. Strange. Nothing at as indicated should happen in the tutorial.

59 Flares Twitter 12 Facebook 34 Google+ 13 LinkedIn 0 StumbleUpon 0 Filament.io 59 Flares ×