Chapter 16. 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 16-1 lists some of the protocols implemented by the libnet modules.
Table 16-1. Protocols implemented by the libnet modules
In this chapter, we discuss Net::SMTP and Net::POP3. Chapter 17, "Usenet News" talks about Net::NNTP, Chapter 18, "FTP" discusses Net::FTP, and Chapter 19, "Lightweight Directory Access with Net::LDAP" covers Net::LDAP. Other libnet modules, such as Net::SNPP and Net::Time, are not described in this book, 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, which can be found on CPAN as the MailTools collection. Other interesting mail-related modules include Mail::Folder and its subclasses, Mail::RBL, Mail::Audit, and Unix::AliasFile. This chapter describes the following subset of the Mail modules:
The rest of this chapter describes the Net modules and the Mail modules.
16.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.
16.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.
16.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: <firstname.lastname@example.org> 250 email@example.com ... Sender ok
RCPT To: <firstname.lastname@example.org> 250 email@example.com ... Recipient ok
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.
184.108.40.206. Net::SMTP methods
The following methods are defined by Net:SMTP.
$smtp = Net::SMTP->new(host[, options])
Constructor. Takes the hostname of the remote mail server, host, and possibly some options, and creates a new SMTP object. Any options are passed to new as a hash, in which the option is the key. The possible options are:
my $banner = $smtp->banner( ); print "$banner\n";
Starts sending the body of the current message to the server. If specified, bodydata can be a list or a reference to a list; the contents of the list and the termination string .\r\n are sent to the server. Returns true if accepted.
If bodydata is not specified, then a true result means that the server is ready to receive data, which must be sent with the datasend and dataend methods (inherited from Net::Cmd).
Here's an example that uses datasend and dataend:
@list_data = (1..10); $smtp->data( ); $smtp->datasend(@list_data); $smtp->dataend( );
$help_text = $smtp->help([subject])
mail can take some ESMTP options, passed as key/value pairs. See the Net::SMTP documentation for the details.
$smtp->recipient(addr[, addr[, ...]])
Tells the server to send the current message to all specified recipients. As defined in the RFC, each address is sent as a separate command to the server. If the sending of any address fails, the process aborts and returns false; you can then call reset to reset the server.
16.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 can get specific messages by message number.
A POP session to retrieve a mail message is negotiated with the server through the following steps:
The following methods are defined by Net:POP3.
$pop = Net::POP3->new([host,] [options])
Constructor. Creates a new Net::POP3 object. host is the name of the remote host to which a POP3 connection will be made. If host is not specified, then the POP3_Host specified in Net::Config is used.
options are passed as key/value pairs, in which the option is the key. The possible options are:
If called with an argument, returns the size of message msgnum. If called without an argument, returns a hash reference, in which the keys are the message numbers of all undeleted messages, and each corresponding value is the size of the message.
Sends both USER and PASS commands. If the password, pass, is not given, then Net::Netrc is used to look up the password based on the host and the username, user. If user is not specified, the current username is used. Returns the count of messages in the mailbox, or undef if the server can't authenticate the user.
Note that if a Net::POP3 object goes out of scope before quit has been called, reset is called before the connection is closed, and any messages marked for deletion are not deleted.
Copyright © 2002 O'Reilly & Associates. All rights reserved.