Site Feed

Tuesday, December 11, 2007

Mail a Text File from the Shell

Would you like an easy way to send files to someone outside of your local network, or quickly test a mail server?

Well the old "mail" command is still available, and it comes in handy for sending config files, or using within a shell script.

This is all you need:

$ mail -s test < /etc/hosts

That's the shortcut. If you want more details, you can read the rest of the post...

Using the mail command

1) Use Mail Interactively to read mail
a) type Mail
b) enter the number of the message to read, press enter
c) press space to page down, n for next message
d) ? for help
e) q to quit

2) Use mail Interactively to send mail
a) type mail command followed by email address
b) Enter subject, press enter
c) Enter text of message
d) press Ctrl-d on a line by itself when finished
e) Enter CC: if desired, or press Ctrl-d again

$ mail
Subject: test
Here is my test message
Cc: <Ctrl-d>$

For a little less work, add the subject to the command prompt. Remember to use quotes if the subject it more than one word.

$ mail -s "mounting"

Can you help me troubleshoot my mount problem?

Cc: <Ctrl-d>$

3) Use mail to Send a Text File

Imagine that you just asked your friend to help you troubleshoot your partitions, and then he asked you to send a copy of your fstab file. Your first thought may be, "how can I copy and paste this into my mail program?", or "how can I get this file to my Windows machine where my mail program is running?" If you have synergy running, it's easy enough -- Then you think, I can use firefox, and attach the file to an email, or paste the text into the message, but that takes a few steps. And then what if X isn't running?

Sure you could copy the file to an nfs mounted partition, or scp the file to another machine that has X or Windows running. But I think one of the easiest ways to email a text file is to use a single mail command from a shell prompt:

$ mail -s "My fstab File" < /etc/fstab

Remember to only send text files, unless you want to uuencode a binary file, but that is usually not the most practical way to move binaries. That sounds like a topic for another day...

Summary of the mail program

In summary, here are three common ways to use Mail / mail:

1) Interactively read: Mail
2) Interactively send: mail <ctrl-d> to end
3) Automated send: mail -s "My fstab File" < /etc/fstab

Note that option 3 may come in handy for testing a recently installed mail server, such as may be required on an RHCE exam. It is also good for mailing log output, which is what logwatch does.

A Little Background

You may be familiar with the Mail command (upper-case "M"), which is convenient for reading mail on the system when pine or mutt hasn't been installed. The the little mail command (lower-case "m") is very convenient for sending mail from the command prompt with a simple one-liner command.

On modern linux systems Mail and mail may be the same program but historically they were different programs. On your system you can check for the mail program:

[ /]$ which mail
[ /]$ which Mail
[ /]$ ll /usr/bin/Mail
lrwxrwxrwx 1 root root 14 Sep 20 2006 /usr/bin/Mail -> ../../bin/mail
[ /]$

Note that /usr/bin/Mail is a symlink to /bin/mail on this rhel4 system.

No comments: