14.2 The Mail ModulesThe Mail modules operate at a higher level than the Net modules, interacting with external mail packages such as mail , mailx , sendmail , or a POP3 server in the case of POP3Client. This section describes some of the MailTools modules, Mail::Folder, and Mail::POP3Client. 14.2.1 Send Email with Mail::MailerThe Mail::Mailer module interacts with external mail programs. When you use Mail::Mailer or create a new Mail::Mailer object, you can specify which mail program you want your program to talk to: Another way to specify the mailer is:use Mail::Mailer qw(mail); whereuse Mail::Mailer; $type = 'sendmail'; $mailprog = Mail::Mailer->new($type);
$type
is the mail program. Once you've created a new object,
use the
open
function to send the message headers to the mail program as a hash
of key/value pairs, where each key represents a header type,
and where the value is the value of that header:
This code represents headers where the recipient of the mail message is you@mail.somename.com , the mail was sent from me@mail.somename.com , and the subject of the mail message is " working? "# mail headers to use in the message %headers = ( 'To' => 'you@mail.somename.com', 'From' => 'me@mail.somename.com', 'Subject' => 'working?' );
Once You then send the body of the message to the mail program:$mailprog->open(\%headers); Now, close the program when the message is finished:print $mailprog "This is the message body.\n"; A practical example of using Mail::Mailer might be a command-line-driven application that works much like the Unix mail program, either reading STDIN until EOF or mailing a file specified on the command line.$mailprog->close; Mail::Mailer uses the environment variable PERL_MAILERS to augment or modify the built-in mailer selection. PERL_MAILERS is specified in the following format: The possible types are listed for the"type1:mailbinary1;mailbinary2;...:type2:mailbinaryX;...:..."
new
method below.
The following methods are defined in Mail::Mailer: 14.2.2 Better Header Control with Mail::Send
Mail::Send is built on top of Mail::Mailer, which means that you can
also choose the mail program that sends the mail. Mail::Send has
implemented the methods
Mail::Send uses the serves the same purpose as:# Start mailer and output headers $fh = $msg->open('sendmail'); This code tells Mail::Send to use sendmail as the mail program.# use sendmail for mailing $mailer = Mail::Mailer->new('sendmail)';
Mail::Send also provides the Therefore, to address a message to you@mail.somename.com :$msg->set($scalar, @array); The above sets the$msg->set('To', 'you@mail.somename.com');
To
header to
you@mail.somename.com
;
however, the following
sets the
To
header to
postmaster@mail.somename.com
and
you@mail.somename.com
, because they represent an array of values.
You might think that you could use the$msg->set('To', ('you@mail.somename.com', 'postmaster@mail.somename.com'));
set
function as follows to add multiple values to a header value:
However,$msg->set('To', 'you@mail.somename.com'); $msg->set('To', 'someone@their.mailaddress.com');
set
doesn't append information from one call to another, and the
example above would send the mail only to
someone@their.mailaddress.com
.
To append a name to the
To
header, use the
add
method. For
example:
$msg->add('To', 'you@mail.somename.com'); $msg->add('To', 'someone@their.mailaddress.com'); The following methods are defined for Mail::Send: 14.2.3 Handle Folders with Mail::FolderOnce you've begun downloading and reading your mail from a POP server, you might want to save or categorize your messages into folders, which allow you to add, delete, save, and move messages easily. You can do this with Mail::Folder, which was written by Kevin Johnson as an object-oriented, folder-independent interface to mail folders. Mail::Folder supports a number of mailbox formats with the following interfaces:
If you are interested in writing a folder interface, see the documentation for the module. The documentation explains the concepts and issues involved, and describes some of the methods you may need to override. The following methods are defined for Mail::Folder:
14.2.4 Handle Messages with Mail::Internet
Mail::Internet implements a number of helpful functions for manipulating
a mail message. These include reads a mail message from STDIN (using a reference to an array). The following example reads a mail message from a filehandle, FILE.@lines = <STDIN>; $mi_obj = new Mail::Internet([@lines]); Theopen(FILE, "/home/nvp/Mail/nvp"); $mi_obj = new Mail::Internet(\*FILE); close(FILE);
print_header
function outputs the header of a message to a file
descriptor; the default is STDOUT.
The above example might output:open(FILE, "/home/nvp/Mail/nvp"); $mi_obj = new Mail::Internet(\*FILE); close(FILE); $mi_obj->print_header(\*STDOUT); whereFrom nvp Mon Jun 9 00:11:10 1997 Received: (from nvp@localhost) by mail.somename.com (8.8/8.8) id AAA03248 for nvp; Mon, 9 Jun 1997 00:11:09 -0500 (EST) Date: Mon, 9 Jun 1997 00:11:09 -0500 (EST) From: "Nathan V. Patwardhan" <nvp> Message-Id: <199706090511.AAA03248@mail.somename.com> To: nvp Subject: pop test X-Status: X-Uid: 1 Status: RO
print_body
also takes a file descriptor as an argument, but only
outputs the body of the message, whereas the
print
function outputs an
entire message.
14.2.5 Mail::Internet Reference
14.2.6 Parse Email Addresses with Mail::AddressMail::Address parses RFC 822-compliant mail addresses of the form: For example, under RFC 822, an address might be represented as:"Full Name or Phrase" <username@host> (Comment Area) or:"Nathan V. Patwardhan" <nvp@mail.somename.com> (No Comment) The Mail::Address constructor parses an email address into three parts based on the categories shown above:"Nathan V. Patwardhan" <nvp@mail.somename.com> Mail::Address also outputs portions of the mail address with the functions$addr = Mail::Address->new("Full Name or Phrase", "username@host", "(Comment Area)");
phrase
,
address
,
comment
,
format
,
name
,
host
, and
user
. The
phrase
,
address
, and
comment
functions represent the first, second,
and third entities that were passed to the Mail::Address constructor, where
the
phrase
function:
outputs:print $addr->phrase(); TheNathan V. Patwardhan
address
function:
outputs:print $addr->address(); And thenvp@mail.somename.com
comment
function:
outputs:print $addr->comment(); A real mail address can be "unmangled," or parsed from its user@somehost.com format, with theNo Comment
user
and
host
functions. The
user
function removes everything starting with the
@
to the
end of the address, and
host
removes everything up to and including
the
@
. Using the previous example of
nvp@mail.somename.com
, the following line:
outputs:print $addr->user; And the following line using thenvp
host
function:
outputs:print $addr->host; nvp@mail.somename.com 14.2.8 Reading Email with Mail::POP3ClientMany networks have machines dedicated to sending and receiving email. Since users might hold accounts on foo.bar.com , while mail is sent to pop-server.bar.com , there must be a means to transfer the mail from the "post office" machine to the host on which the user works. The Post Office Protocol, or POP, negotiates this mail transfer. When a user wants to retrieve his or her mail, the user's mail client connects to the POP server and authenticates the user with a login name and password, as we described earlier. Once authenticated, the user can list, read, and delete messages from the POP server. The Mail::POP3Client module simplifies the process of "talking POP" by implementing a number of functions to login, parse, and read mail messages held on the POP server. Information that the POP server needs, such as the user's login name and password, and possibly some optional information (the name of the POP host, the port, and a debugging flag) are passed to the constructor when a new POP3Client object is created. For example: POP3Client provides functions to perform the following:use Mail::POP3Client; $pop = Mail::POP3Client->new("login", # required "password", #required "pophost.your.domain", # not required port, # default is 110 debug_flag); # any positive integer 14.2.8.1 Counting messages
The You can also use a regular expression to parse the headers and display certain information, such as the sender and subject of each mail message:use strict; use Mail::POP3Client; my($pop, $num_mesg, $i); $pop = Mail::POP3Client->new("nvp", "xxxxxx"); $num_mesg = $pop->Count; # How many messages are there? print("You have ".$num_mesg." new message(s).\n"); for ($i = 1; $i <= $num_mesg; $i++) { print $pop->Head($i), "\n"; #print header for each message } my($pop, $num_mesg, $i); $pop = Mail::POP3Client->new("nvp", "xxxxxx"); for ($i = 1; $i <= $pop->Count; $i++) { foreach ($pop->Head($i)) { # output from and subject print $_." " if /^(From|Subject)/; } print "\n"; } 14.2.8.2 Getting and setting the host and port
The returns the current POP host. To set the new POP host to new-pop.bar.com , you could do this:$obj->Host; The$new_host = 'new-pop.bar.com'; $obj->Host($new_host);
Port
method works like
Host
, returning or
setting the current port that the POP server is bound to:
$obj->Port; #return the current port for the POP server $new_port = 7000; #set the port to 7000 $obj->Port($new_port); 14.2.8.3 Retrieving the message body
Naturally, you'll want to read more than the headers of your mail
messages, so you'll want to use the 14.2.8.4 Deleting and undeleting messages
Messages can be deleted from the POP mailbox with the Like most mail programs, Mail::POP3Client can undelete messages that have been marked for deletion. Use the$pop->Delete(1); #delete the first message
Reset
method to do this:
$pop->Reset(1); #undelete the first message 14.2.8.5 Checking the connection
Most programs that require a user to log in will time out after a certain
period of time for security and resource reasons. The 14.2.8.6 Explicitly opening and closing connections
POP connections can be explicitly opened and closed with |
|