LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to Linux Subscribe to Newsletters

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.


Have any tips or tricks for command line mail? Share them here.
You must be logged in to the O'Reilly Network to post a talkback.
Post Comment
Full Threads Oldest First

Showing messages 1 through 5 of 5.

  • bcc: the "b" stands for "blind" not "blank"
    2003-05-23 12:29:53  anonymous2 [Reply | View]

    bcc == Blind Carbon Copy, meaning that that the other recipients are don't get to see who's receiving bcc's.
    • chromatic  photo bcc: the "b" stands for "blind" not "blank"
      2003-05-27 15:17:18  chromatic | O'Reilly AuthorO'Reilly Blogger [Reply | View]

      Looking around, I've seen it both ways. I've modified the article to reflect both uses.

      Thanks for the feedback.
  • How to pass through proxy servers?
    2003-05-22 23:31:31  anonymous2 [Reply | View]

    My ISP has a proxy server, which doesn't allow me to access other smtp servers on the Net directly. How can I pass this with a telnet session?
    • How to pass through proxy servers?
      2003-05-26 10:58:49  anonymous2 [Reply | View]

      It depends on a number of factors that are going on. Have you correctly diagnosed that the problem is with the proxy server itself?

      Some pop and stmp servers don't give ascii responses such that you get only a return code. Except for your own messaging and the server's return code response your telnet session would appear to be one way which is not the case.

      Proxy servers can block telnetting into pop and smtp servers by looking for the identity header that an email client normally sends. If they don't see it then they make an arbitrary decision to block your transmission. Think of it as an anti-spam, anti-harrasment mechanism since isp's are normally shot out of the water when they have naughty clients who play dirty email tricks. The easiest way to verify this would be to configure your email client to use a pseudo proxy (using a tcp redirector while you sniff with etheral) on your own machine which redirects the communication through the isp's proxy server. That way you can read the messages that are being exchanged between email client and proxy.

      Dru Lavigne tells me that her next article will be about proxies and she promised to discuss this very issue that you've brought up.

      Hope this helps

      Robert Bernier
      • How to pass through proxy servers?
        2003-05-28 06:18:51  anonymous2 [Reply | View]

        I can telnet into the SMTP proxy at my ISP, and send mails out that way. It's not perfect, because it would be nice to "override" the ISP's proxy, and communicate directly with the other SMTP server...

        I suppose that option will be rather difficult to implement, but it works now anyway...

        Thanks,

        Raf


Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com