home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: 13.2 The IO::Socket Module Chapter 14 Next: 14.2 The Mail Modules
 

14. Email Connectivity

Electronic mail is arguably the most essential Internet application. In fact, for many people, it's their introduction to the Internet. Thus the Perl modules that deal with email are among the most useful modules. There are two major groups of modules that provide email capabilities. The first group is Graham Barr's libnet collection, which contains packages for developing client-side applications over the Internet in Perl. Table 14.1 lists some of the protocols implemented by the libnet modules.


Table 14.1: Protocols Implemented by the libnet Modules
Protocol Module Description
POP3 Net::POP3

Post Office Protocol, for reading email

SMTP Net::SMTP

Simple Mail Transfer Protocol, for sending email

FTP Net::FTP

File Transfer Protocol, for transferring files between hosts

NNTP Net::NNTP

Network News Transfer Protocol, for reading Usenet news

In this chapter, we discuss Net::SMTP and Net::POP3. Chapter 15, Usenet News , talks about Net::NNTP, and Chapter 16, FTP , discusses Net::FTP. Other libnet modules, such as Net::SNPP and Net::Time, are not described here, but you can get information about them from CPAN or with the perldoc command if libnet is installed on your system.

The second group of mail-related modules are the Mail modules, many of which were also written by Graham Barr. They can be found on CPAN as the MailTools collection. The Mail modules also include Mail::Folder and its subclasses, written by Kevin Johnson, and Mail::POP3Client, by Sean Dowd. This chapter describes the following subset of the Mail modules:

Mail::Send

Built on top of Mail::Mailer, providing better control of mail headers.

Mail::Mailer

Interacts with external mail programs to send mail.

Mail::Folder

Provides a base class and subclasses to work with mail folders.

Mail::Internet

Provides functions to manipulate a mail message.

Mail::Address

Extracts and manipulates RFC 822-compliant mail addresses.

Mail::POP3Client

Provides an interface to a POP3 server, based on RFC 1081.

The rest of this chapter describes the modules; first the Net modules and then the Mail modules.

14.1 The Net Modules

Net::SMTP and Net::POP3 are the modules for sending and receiving email via the SMTP and POP3 protocols. When you use these modules, you are working at the socket level; they directly implement the Internet protocols for sending and receiving mail as defined in the relevant RFCs - RFC 821 for SMTP and RFC 1081 for POP3.

14.1.1 Send Email with Net::SMTP

The Simple Mail Transfer Protocol, or SMTP, is responsible for clients negotiating RCPT ("to") and FROM ("from") requests with an SMTP server, sending data to the SMTP server, and then sending an end-of-data indicator. Net::SMTP is a subclass of Net::Cmd and IO::Socket::INET that implements an interface to the SMTP and ESMTP protocols. These protocols send mail by talking to an SMTP server through a socket, as described in RFC 821.

When would you want to use Net::SMTP instead of sending mail with an external program? Since socket communications don't involve spawning an external program, your programs won't suffer from the overhead associated with running an extra process. Talking to SMTP is convenient for sending a volume of mail messages. Naturally, your server must have an SMTP server running or a remote mailhost must allow you to talk to it; otherwise you won't be able to use this module. That's when you can turn to Mail::Mailer or Mail::Send and let them provide an interface to an external mail program for you. This is the case, for example, with home computers, which don't generally run their own SMTP server.

14.1.2 The SMTP Protocol and the SMTP Session

The SMTP protocol defines the set of commands a client sends to an SMTP server, which is generally bound to port 25 of a mailhost. Requests and responses are negotiated between client and server.

When a client negotiates an SMTP session with a server, the server tells the client that it's listening. Once you're connected, you introduce yourself to the server by issuing a HELO command. The HELO command accepts one parameter - your hostname - and defaults to your remote hostname if you don't specify one. If the command is successful, the server sends a 250 response, as follows:

HELO
250 mail.somename.com Hello some-remote-host.com [127.0.0.1], pleased to meet 
you
After you've been greeted by the server, send the MAIL command to tell the server who the message is from. The MAIL command takes the string From: user@hostname as an argument, and the server responds with a 250 message to indicate success:
MAIL From: <realuser@realhost.com>
250 realuser@realhost.com ... Sender ok
Then you send the RCPT command to tell the server who the recipient is:
RCPT To: <nospam@rid-spam-now.com>
250 nospam@rid-spam-now.com ... Recipient ok
Now you're ready to send the body of your message to the server. The DATA command tells the server that all data until a . on a line by itself is to be treated as the body of the mail message:
DATA
354 Enter mail, end with "." on a line by itself
Subject: Hi, just thought you'd be interested ...

Hi, this is the text of my mail message that I'm going to 
send with Net::SMTP to show you how it works.
.
250 VAA09505 Message accepted for delivery
Once again you get a 250 response, indicating that the message has been accepted for delivery. At that point, you can exit the SMTP session with the QUIT command, which returns 221 on success:
QUIT
221 mail.somename.com closing connection
Connection closed by foreign host.

14.1.3 Retrieving Email with Net::POP3

You can use SMTP to send mail, but not to retrieve it. For retrieving messages, use the Post Office Protocol version 3 (POP3), described in RFC 1081. One way to do this is to use the Net::POP3 module. POP3 provides commands for interacting with the POP server, typically bound to port 110. Net::POP3 automates the transfer of email from a remote server to the local machine.

The POP server retrieves messages from a specified spooling directory on the remote system. The messages are stored in a file named for the username; anonymous logins are not permitted. Authentication is based on username and password and is done by sending the USER and PASS commands to the POP server. For example, identification of user foo with password bar looks like this:

USER foo
PASS bar
Net::POP3 has user and pass methods but may also authenticate users with login , which takes both username and password arguments. If authentication fails, the user cannot retrieve, delete, or alter any messages from the mail server. login returns the number of messages on the POP server for the user, or undef if authentication fails.

Authenticated users can retrieve information about their mailboxes, and they can get specific messages by message number.

A POP session to retrieve a mail message is negotiated with the server like this:

  1. Connect to the POP server (the default port is 110).

  2. Send USER command.

  3. Send PASS command.

  4. If authenticated, receive number of messages.

  5. Send RETR < message number > command to retrieve a specific message.

  6. Send QUIT command to end session.

The following methods are defined by Net:POP3:







??????????????@Mail.ru