To send and receive e-mails that we are accustomed to Web-based services such as Gmail, Yahoo etc or desktop-based email clients. However, on Linux, you can easily send / receive emails from the command line as well. This is useful in situations such as sending mail from shell scripts or other type of program.
This tutorial explains how to use the mail command on linux for sending and receiving 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:sender@example.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<sender@example.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 electronic control used so far is quick and easy, but unfortunately it does not support sending attachments. Now we go a step further and introduce MUTT. In fact, Mutt is a very powerful email client command line and capable of doing more than sending emails with attachments, but for now we will focus only on basic attachments.
If Mutt is not installed on your box, you can use apt-get or yum command as root (or use sudo place).
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
1
2
|
#!/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:
1
2
3
|
#!/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:
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.