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.
Post Office Protocol, for reading email
Simple Mail Transfer Protocol, for sending email
File Transfer Protocol, for transferring files between hosts
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:
Built on top of Mail::Mailer, providing better control of mail headers.
Interacts with external mail programs to send mail.
Provides a base class and subclasses to work with mail folders.
Provides functions to manipulate a mail message.
Extracts and manipulates RFC 822-compliant mail addresses.
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.
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.
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.
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:
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 stringHELO 250 mail.somename.com Hello some-remote-host.com [127.0.0.1], pleased to meet you
From: user@hostnameas an argument, and the server responds with a 250 message to indicate success:
Then you send the RCPT command to tell the server who the recipient is:MAIL From: <firstname.lastname@example.org> 250 email@example.com ... Sender 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 aRCPT To: <firstname.lastname@example.org> 250 email@example.com ... Recipient ok
.on a line by itself is to be treated as the body of the mail message:
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: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
QUIT 221 mail.somename.com closing connection Connection closed by foreign host.
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
foo with password
bar looks like this:
Net::POP3 hasUSER foo PASS bar
passmethods 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.
loginreturns the number of messages on the POP server for the user, or
undefif 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:
Connect to the POP server (the default port is 110).
Send USER command.
Send PASS command.
If authenticated, receive number of messages.
Send RETR <message number> command to retrieve a specific message.
Send QUIT command to end session.
The following methods are defined by Net:POP3:
Copyright © 2001 O'Reilly & Associates. All rights reserved.