LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


Command-Line Email
Pages: 1, 2

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.


Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!


Linux Resources
  • Linux Online
  • The Linux FAQ
  • linux.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: