Testing the mail command on Linux.
1. The Simplest Example
mail -s test yangfang@fudan.edu.cn
This command sends an email with the subject test and an empty body to the address that follows. If the system already has an MTA (Mail Transfer Agent, such as Postfix, Sendmail, Exim, etc.) configured, and the target mailbox does not block this somewhat odd-looking message, the email should arrive.
If you do not want to be disturbed by this kind of test email, it is better to change the recipient to a local account, for example:
mail -s test localuser
Here, localuser is an existing username on the local machine.
2. Three Common Sending Formats
Entering the Message Body Directly in the Current Shell
mail -s test yangfang@fudan.edu.cn
After running it, you can use the current shell as a simple editor: type the message body, then press Ctrl-D when you are done to end input and send the email.
Sending the Message Body Through a Pipe
echo "mail content" | mail -s test yangfang@fudan.edu.cn
This is the method I use most often, probably because I like pipes.
Using the Contents of a File as the Message Body
mail -s test yangfang@fudan.edu.cn < file
This sends the contents of file as the email body.
In fact, it is easy to see that mail reads the message content from standard input and then sends the email. That is the main point I wanted to explain. With that, the second example is done.
3. Sending Email with Attachments
If the system does not have the uuencode command, you can install sharutils first. For example, on systems that use yum:
yum install sharutils
uuencode usually requires two parameters: the first is the file to send, and the second is the filename displayed in the email. It can also be used together with pipes.
Send a single file:
uuencode /home/yfang/a.gif a.gif | mail -s pictest yangfang@fudan.edu.cn
Package and compress a directory before sending it:
tar czf - /home/yfang/ | uuencode home.tgz | mail -s pictest yangfang@fudan.edu.cn
Note: implementations of mail or mailx may differ across distributions. Some versions support the -a option for adding attachments directly; for example, Slackware's mail seems to have this option, though I have not used it. Before using it in practice, you can check the local help first:
mail --help
man mail
Reposted from: http://www.svn8.com/linux/accidence/20091016/10858.html
