Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Command-Line Email

by Robert Bernier
05/22/2003

When my editor asked me to write an article on email, my first reaction was to mimic a sound I used to hear on Sesame Street. Come on now; what can be written about email that hasn't already been said? What can children and grandparents both do at the drop of a hat? What easier way is there to share a joke or comment on the latest goings-on? What more efficient and flexible monitoring and report tool does there exist for the system administrator?

However, back in my early years I was confused to no end by all those terms that I had to know stone cold. Obscure combinations of letters and numbers had to be memorized. There's so much out there on email that one could spend a good chunk of his inheritance exploring the technology.

History
Tools of the Trade
Tricks of the Trade
Conclusion

History

In the beginning, there was the word and the word was email.

Email's mode of transmission is based on 7-bit ASCII. In other words, early Internet communication was only 128 characters — specifically numbers and letters — that we use in English. Hence no Icelandic, Chinese, Hebrew or Greek, to name a few languages. Also, there were no pictures, or sound clips. The only messages sent were English text.

Related Reading

Sendmail
By Bryan Costales

The mid-1990's was a time of evolution. The ordinary person discovered the Internet and the Internet discovered a new purpose. The first RFCs (Requests For Comments) were coming out, describing a standard for email transmissions of images, sounds, and binaries that would overcome the 7-bit ASCII limitations that had been adopted all those years before. MIME or Multipurpose Internet Mail Extensions, was a new standard meant to succeed the patchwork of binary-to-ASCII solutions.

Tools of the Trade

Graphical and Console Client Tools

There are two major divisions of email clients, graphical and console.

Graphical clients are best used when the content, be it text or multi-media, is processed by a person, and human factor plays an important function in both as the sender and receiver of the communication.

Console clients do the same thing as their graphical counterparts, but there is less multimedia support. A console client's great strength is its economical usage of machine resources; it lends itself well to scripting.

Basic Configuration

Typically there are four links in the email chain:

  1. Composition with a mail user agent (MUA), such as Mozilla. Creating an email is a painless procedure with most programs. It's simply a matter of filling in the blanks.

    • The address to which to send the message.
    • The subject matter.
    • The message, which can include all manner of information including text, sound, and graphics.
    • The return address, your email address.

  2. Delivering the message using a message transfer agent (MTA); sendmail is a good example. Configuring the MTA is normally a one-time issue that happens when using the email client for the first time.

  3. Storing a message destined for a recipient -- the POP (Post Office Protocol) server accesses the message delivered by the MTA. Small ISPs and office intranets often have the same server running both the POP and SMTP (Simple Mail Transfer Protocol) servers. Larger organizations separate the duties, normally for a combination of performance and security reasons.

  4. Message retrieval, once again using the ubiquitous mail user agent.

There are two competing protocols used:

Back in those early days, the easiest way for people to learn to email was to associate specific actions with real-world equivalents. Take, for example, the addressing of an email:

  1. The subject
  2. Main recipients
  3. Courtesy recipients
  4. Privately informed recipients

Hence the terms subject, to, cc (carbon copy), and bcc (blind or blank carbon copy). For those people who were born after the invention of electricity, carbon copy refers to a special composite of paper and ink that when pressed hard by a writing implement, such as a pen, it would produce a number of copies of the same information on several layers of paper. That's how Bill Gates signed his first contract with IBM.

Tricks of the Trade

Telnetting into the SMTP and POP servers

I had a student from the old Soviet Union. She was quite bright; in her homeland she used to program directly into machine language, and I do mean 1s and 0s. Her father-in-law was one of the moon drivers for the Luna moon missions. Another interesting habit of hers was to email her husband by using a telnet session into both the SMTP and POP servers.

SMTP and POP3 are among a number of protocols that use a pseudo-English language. The original intention was to allow anybody to interface with a server directly and carry out a communication session — to send and receive email.

Just for the fun of it, I replicated what she did in a telnet session on my own servers.

Here's a subset of SMTP:

Protocol CommandDescription
HELO localhost.localdomain Initiate communication with SMTP server
MAIL FROM: user@addr.com Specify who is sending the email
RCPT TO: user@addr.com Specify who is to receive the email
DATA Specify data of the email (any number of lines)
. A period alone specifies the end of the email
QUIT Close the connection

Here's a sample SMTP session from my own machine. After I've connected, all of the SMTP server's responses start with numeric codes.

[bernier@wolf bernier]$
telnet localhost 25
Trying 127.0.0.1...
Connected to wolf (127.0.0.1).
Escape character is '^]'.
220 localhost.localdomain; ESMTP Thu, 6 Mar 2003 17:57:01 -0500
helo localhost
250 localhost.localdomain Hello wolf [127.0.0.1], pleased to meet you
mail from: bernier@localhost
250 2.1.0 bernier@localhost...  Sender ok
rcpt to: robert.bernier5@sympatico.ca
250 2.1.5 robert.bernier5@sympatico.ca... Recipient ok
data 
354 Enter mail, end with "." on a line by itself
subject; I'm hungry, send pizza
Please send me one large pepperoni all dressed.
Don't forget the pineapples!
.
250 2.0.0 h26N3hs18884 Message accepted for delivery
quit
221 2.0.0 localhost.localdomain closing connection
Connection closed by foreign host.

Here's a subset of the POP3 protocol:

Protocol CommandDescription
USER username Identify the user
PASS password Type in the password
LIST Lists all emails available
RETR # Retrieval of a given email
DELE # Mark a given email for deletion upon clean logout
QUIT Close the session cleanly

Here's the telnet session from my ISP's POP server:

 bash-2.05$ telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.localdomain
Escape character is '^]'.
+OK InterMail POP3 server ready.
user bernier
+OK please send PASS command
pass 1234
+OK bernier is welcome here
list
+OK 1 messages
retr 2
+OK 767 octets
Return-Path: <root@localhost.localdomain>
Received: from localhost.localdomain ([67.69.160.216]) by
tomts5-srv.bellnexxia.net (InterMail vM.5.01.04.19
201-253-122-122-119-20020516) with ESMTP id
<20030307030259.NLJD29897.tomts5-srv.bellnexxia.net@localhost.localdomain>
for <robert.bernier5@sympatico.ca>;
Thu, 6 Mar 2003 22:02:59 -0500
Received: (from bernier@localhost) by localhost.localdomain
(XXX/linuxconf) id h2730mA01858
for robert.bernier5@sympatico.ca; Thu, 6 Mar 2003 22:00:48 -0500
Date: Thu, 6 Mar 2003 22:00:48 -0500
From: root <root@localhost.localdomain>
Message-Id: <200303070300.h2730mA01858@localhost.localdomain>
To: robert.bernier5@sympatico.ca
Subject: I'm hungry send pizza

Please send me one large pepperoni all dressed.

Don't forget the pineapples!
.

Using telnet is the best way to figure out why email doesn't work. You will be informed the exact reason from the server why there's a failure, unlike the error messages that you get from clients such as, for example, Microsoft outlook. Many help-desk technicians receive some form of training in the POP and SMTP protocols.

Sending Standard Output as an Email Message

This is such a useful little trick. We had an employee that worked from home on a dial-up connection. Every Friday night he had to leave his machine on so some automated tasks could take place. His machine was therefore configured to send an email with his IP address about 10 minutes prior to the maintenance mode starting. The cron job on his machine consisted of a one-line command similar to the one below. (Be careful about the use of cut; the number of digits for the IP address range was already known, so we were able to specify the address length exactly.)

echo `/sbin/ifconfig | grep -A 1 eth0 | grep inet \
	| cut -c21-33 ` | mail -s employeeX admin@superserver.com

Encoding Binaries to ASCII

Have you ever used shar or uuencode? Both utilities are designed to encode binary information to the ASCII character set. The shar utility has a built-in script that will return the encoded information to its original binary format by running as a shell command. Both tools were once crucial to sending binary information across the Internet via email.

Suppose you want to encode a 38Kb binary image called mykitty.jpg. You can do it by trying the following:

$ cat mykitty.jpg | uuencode - > mykitty.ascii.uu

$ uudecode -o mykitty.jpg mykitty.ascii.uu Returns it to binary form

$ shar mykitty.jpg > mykitty.ascii.sh

$ sh mykitty.ascii.sh Returns it to binary form

Did you notice that the ASCII-encoded files are larger? Remember, when you send a 1Mb zipped file across the Internet, you are sending a file that is significantly larger than what presently sits on your computer.

Sending a Large File as a Series of Smaller Ones

I knew a sysadmin who worked on contract with several firms. He would often keep track of his machines by having automated reports sent to his personal account supplied by his ISP. There was, however, a bit of a problem: although he had plenty of space allotted to his account, he was limited to each individual email being no larger than 5Mb. His solution was to use shar.

Here is a typical listing of the files after the archives have been created for a contract he called companyMega:

companyMega.01 companyMega.06 companyMega.11 companyMega.16 companyMega.21
companyMega.02 companyMega.07 companyMega.12 companyMega.17 companyMega.22
companyMega.03 companyMega.08 companyMega.13 companyMega.18 companyMega.23
companyMega.04 companyMega.09 companyMega.14 companyMega.19 companyMega.24
companyMega.05 companyMega.10 companyMega.15 companyMega.20 companyMega.25

Here's a snippet of code that was run as part of a cron job:

shar -L 4000 -o companyMega companyMega.logs.tar.gz

Now loop and mail the individual archives through a script called sendoff.sh:

for u
do
echo attachment backup for $u | mutt -a $u -s $u bernier@myisp.com
done

The script was invoked in the active directory as follows:

sh sendoff.sh `ls companyMega.*`

An alternative to mutt is the old standby mail. The script could also be written:

cat $u | mail -s bakup:$u bernier@myisp.com

The difference between this command and the former is by using mutt, I've sent off the backup as a MIME attachment. That is a bit cleaner to deal with for those of the non-scripting persuasion — just manually save the attachment into a directory of your choosing.

Reassembling the file is trivial:

unshar -e companyMega.*

Conclusion

Email is as important as the telephone. Security is the buzzword on the Internet these days. My next article will continue this discussion on email and demonstrate how to carry out authentication and encryption with several email clients using OpenSSL.

Robert Bernier is the PostgreSQL business intelligence analyst for SRA America, a subsidiary of Software Research America (SRA).


Return to the Linux DevCenter.

Copyright © 2009 O'Reilly Media, Inc.