Wednesday, February 9, 2011

Send Email in Unix / Linux

Most distributions of Unix / Linux come with the mail program installed on it. I will show you how to send an email using the mail program.

Here's a fast-track information on the mail program:

-bash-2.05b# type mail

mail is hashed (/bin/mail)
-bash-2.05b# whatis mail
mail (1) - send and receive mail
-bash-2.05b#
Basic Usage of mail


Let's send an email that specifies the following:

A Subject Line
A List of Recepients
A Message (Body) 
-bash-2.05b# mail -s "Test Email" test,demo


Hi!




Kindly, ignore this test email.
.
Cc: root
-bash-2.05b#
  • -s specifies a Subject (Line) for the message being sent
  • After the subject we specify a comma separated list of recepients
  • Hitting Enter / Return places the cursor on the next line and we can type in our message
  • When we are done, we can type in a single dot (.) on a new line to signal "Go!" or "Send!"
  • However, the mail program may prompt us to include any recepient in the Cc: group (optional)
We can check the message in various ways. The mutt program is an easy to use tool that can be used for viewing and replying to emails. Another way is to directly display the contents of the mail file as shown below:


-bash-2.05b# cat /var/spool/mail/root

From root@host-6-9.linuxzoo.net Wed Feb 9 21:07:58 2011
Return-Path:
Received: from host-6-9.linuxzoo.net (localhost.localdomain [127.0.0.1])
by host-6-9.linuxzoo.net (8.12.11/8.12.11) with ESMTP id p19L7vLP002368;
Wed, 9 Feb 2011 21:07:57 GMT
Received: (from root@localhost)
by host-6-9.linuxzoo.net (8.12.11/8.12.11/Submit) id p19L7vPG002367;
Wed, 9 Feb 2011 21:07:57 GMT
Date: Wed, 9 Feb 2011 21:07:57 GMT
From: root
Message-Id: 201102092107.p19L7vPG002367@host-6-9.linuxzoo.net
To: test@host-6-9.linuxzoo.net, demo@host-6-9.linuxzoo.net
Subject: Test Email
Cc: root@host-6-9.linuxzoo.net

Hi!



Kindly, ignore this test email.

-bash-2.05b#

 The mail program itself can be used for viewing emails and performing actions on them as shown below:

 -bash-2.05b# mail

Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root@host-6-9.linuxz Wed Feb 9 21:07 20/698 "Test Email"
& 1
Message 1:
From root@host-6-9.linuxzoo.net Wed Feb 9 21:07:58 2011
Date: Wed, 9 Feb 2011 21:07:57 GMT
From: root
To: test@host-6-9.linuxzoo.net, demo@host-6-9.linuxzoo.net
Subject: Test Email
Cc: root@host-6-9.linuxzoo.net

Hi!


Kindly, ignore this test email.

& q
Saved 1 message in mbox
-bash-2.05b#

The mail program displays the messages from the default mail file and its & prompt accepts some commands specific to the mail program. In the above example, I have typed 1 to instruct it to display the 1st message. We can type ? to display the commands available.

Advanced Usage of mail


In this section, I will show you how to do more with mail than just sending emails from the command line.

Message Body Containing the Output of Another Command or Job:

uptime | mail -s "System Resource Availability" root -c demo

Here's a view from mutt program of the above message:

-bash-2.05b# su - demo

[demo@host-6-9 demo]$ mutt 

Date: Wed, 9 Feb 2011 21:27:29 GMT

From: root
To: root@host-6-9.linuxzoo.net
Subject: System Resource Availability
Cc: demo@host-6-9.linuxzoo.net

21:27:29 up 5:01, 1 user, load average: 0.15, 0.12, 0.09


Note: -c specifies recepients in the Cc (Carbon Copy) group.


Customized Settings for the mail Program:

We can create a local .mailrc file in the home directory. For example, to disable prompting for Cc: list, we can do the following:

-bash-2.05b# cat > .mailrc

unset askcc
-bash-2.05b# source .mailrc
-bash-2.05b# mail -s "Party Time" demo,test

Hi!

We're going to have party tonight!

Cheers!
.
EOT

Notice the "EOT" (End of Typing or Text) above. We can also hit Ctrl + d to insert the EOT instead of typing in the the dot (.).

To Store Outgoing Emails:
 
set record=$HOME/outgoing

To View the Contents of the "outgoing" (name it anything) file we have several methods:

cat outgoing

or

mail -f outgoing
Example:

First Method:

-bash-2.05b# mail -s "Outgoing Message" demo

Hi!
.
Cc:
-bash-2.05b# ls -l out
-rw-r--r-- 1 root root 76 Feb 11 21:31 out
-bash-2.05b# cat out
From root Fri Feb 11 21:31:21 2011
To: demo
Subject: Outgoing Message

Hi!

Second Method: 
-bash-2.05b# mail -f out
Mail version 8.1 6/6/93. Type ? for help.
"out": 1 message 1 new
>N 1 root Fri Feb 11 21:31 6/76 "Outgoing Message"
& 1
Message 1:
From root Fri Feb 11 21:31:21 2011
To: demo
Subject: Outgoing Message

Hi!

& q
"out" complete
The second method, as shown above, is more efficient.

Some Useful Tips:

The variable $MAIL contains the path to the  "postoffice" (file) which records incoming emails.

echo $MAIL

/var/spool/mail/root
man mail and info mail will give you details on the mail program.  
cat /usr/lib/mail.help contains a short description of the functionality available when using the mail program interactively.