| Previous | Next
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 Net::POP3. This section describes some of the MailTools modules, Mail::Folder, and the other mail-related modules that were mentioned at the beginning of this chapter. 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: use Mail::Mailer qw(mail); Another way to specify the mailer is: use Mail::Mailer; $type = 'sendmail'; $mailprog = Mail::Mailer->new($type); in which # Mail headers to use in the message %headers = ( 'To' => 'you@mail.somename.com', 'From' => 'me@mail.somename.com', 'Subject' => 'working?' ); This code represents headers in which 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?. Once $mailprog->open(\%headers); You then send the body of the message to the mail program: print $mailprog "This is the message body.\n"; Close the program when the message is finished: $mailprog->close; 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 end-of-file or mailing a file specified on the command line. 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: "type1:mailbinary1;mailbinary2;...:type2:mailbinaryX;...:..." The possible types are listed for the The following methods are defined in Mail::Mailer.
$mailer = new Mail::Mailer [type, command] Constructor. Creates a new Mailer object representing the message to be sent. If the optional arguments are specified, the value of
If no arguments are specified, the Mailer object searches for executables in the above order and uses the first one found as the default mailer.
$mailer->close
$mailer->open(\%hashref) Sends message headers to the mail program. The headers are passed via a reference to a hash, in which each key is the name of a header, and the value is the contents of the header field. The value can be either a scalar or a reference to an array of scalars. Better Header Control with Mail::SendMail::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:
This code tells Mail::Send to use sendmail as the mail program. Mail::Send also provides the $msg->set($scalar, @array); Therefore, to address a message to you@mail.somename.com:
The above sets the
You might think that you could use the
However,
The following methods are defined for Mail::Send.
$msg = new Mail::Send [header=>'value'[, ...]] Constructor. Creates a new Mail::Send object that is the mail message you want to send. You can include values for headers when you create the object, or include them later by calling the appropriate methods.
$msg->add(header, values) Adds a header to the message.
$msg->bcc(values) Adds a
$msg->cancel Not implemented yet, but will cancel the message.
$msg->cc(values) Adds a
$fh->close Closes the filehandle
$msg->delete(header) Deletes the header
$fh = $msg->open Opens a filehandle for the message object. The filehandle is a Mail::Mailer object.
$msg->set(header, values) Sets the header
$msg->subject('What this message is about')
Sets the value of the
$msg->to(values) Sets the 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.
$folder = new(ftype[, foldername][, options]) Creates a new Mail::Folder object of the specified type. Arguments are:
$folder->add_label(msg_num, label) Associates the label
$folder->append_message(\$mi_ref) Adds message to a folder. Argument is a reference to a Mail::Internet object.
$folder->clear_label(label) Deletes the association with
$folder->close Does any necessary housekeeping and closes the folder.
$folder->current_message([msg_num]) With no argument, returns the message number of the current message in the folder. With an argument, sets the current message number for the folder to
$folder->debug([value]) Sets the level of debugging information for the object to
$folder->debug_print(text) Prints
$folder->delete_label(msg_num, label) Deletes the association of
$folder->delete_message(msgs) Takes a list of messages,
$folder->dup(msg_num, \$folder_ref) Copies the message specified by
$folder->first_labeled_message(label) Returns the message number of the first message in the folder that has
$folder->first_message Returns the message number of the first message in the folder.
$folder->foldername Returns the name of the folder that the object has opened.
$folder->get_fields(msg_num, fields) Retrieves the fields specified in the list
$folder->get_header(msg_num) Extracts a message header; takes one argument: the message number. Returns a reference to a Mail::Header object.
$folder->get_message(msg_num) Takes a message number as argument and returns a Mail::Internet object reference to that message, or on failure.
$folder->get_message_file(msg_num) Like
$folder->get_mime_header(msg_num) Works much like
$folder->get_mime_message (msg_num[, parserobject][, options]) Returns a MIME::Entity object for the specified message. Calls
$folder->get_option(option) Returns the setting for the specified option, or
$folder->inverse_select(\$func_ref) Returns a list, in no specific order, of message numbers that do not match a set of criteria. The argument,
$folder->is_readonly Returns if the folder has the
$folder->label_exists(msg_num, label) Returns if
$folder->last_labeled_message(label) Returns the message number of the last message in the folder with the label
$folder->last_message Returns the message number of the last message in the folder.
$folder->list_all_labels Returns a list, in no specific order, of all labels associated with messages in the folder. If called in scalar context, returns the number of labels associated with the messages.
$folder->list_labels(msg_num) Returns a list, in no specific order, of all labels associated with
$folder->message_exists(msg_num) Returns if a message with the number
$folder->message_list Returns a list of the message numbers in the folder, in no specific order. The syntax is: print $folder->message_list."\n"
$folder->next_labeled_message(msg_num, label) Returns the message number of the next message in the folder relative to
$folder->next_message([msg_num]) Returns the number of the next message in the folder relative to
$folder->open(foldername) If you didn't specify a folder name in the constructor, you need to call the
$folder->pack For formats that allow message number sequences to have gaps, renames the files in the folders to eliminate any such gaps. May result in renumbering some messages.
$folder->prev_labeled_message(msg_num, label) Returns the message number of the previous message in the folder relative to
$folder->prev_message([msg_num]) Returns the number of the previous message in the folder relative to
$folder->qty Returns the number of messages in the folder. The syntax is: print "There are ".$folder->qty." messages in your folder\n";
$folder->refile(msg_num, \$fldr_ref) Moves messages between folders. Takes a message number and folder reference as arguments.
$folder->select(\$func_ref) Returns a list of messages that meet a set of criteria. The argument,
$folder->select_label(label) Returns a list of messages with the label
$folder->set_option(option, value) Sets the specified option to
$folder->set_readonly Sets the
$folder->sort(\$func_ref) Returns a sorted list of messages.
$folder->sync Synchronizes the folder with internal data structures and reads in any new messages since the last
$folder->undelete_message(msgs) Unmarks a list of messages,
$folder->update_message(msg_num, \$mref) Replaces the message specified by Handle Messages with Mail::InternetMail::Internet implements a number of helpful functions for manipulating a mail message. These include @lines = <STDIN>; $mi_obj = new Mail::Internet([@lines]); reads a mail message from STDIN (using a reference to an array). The following example reads a mail message from a filehandle, FILE: open(FILE, "/home/nvp/Mail/nvp"); $mi_obj = new Mail::Internet(\*FILE); close(FILE); The open(FILE, "/home/nvp/Mail/nvp"); $mi_obj = new Mail::Internet(\*FILE); close(FILE); $mi_obj->print_header(\*STDOUT); The above example might output: From 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 in which Mail::Internet Reference
$mail = new Mail::Internet ([arg], [options]) Creates a new Mail::Internet object.
$mail->add_signature([file]) Appends a signature to the message.
$mail->body( ) Returns the body of the message as a reference to an array. Each entry in the array represents one line of the message.
$mail->escape_from( ) Inserts a leading
$headobj = $mail->head( ) Returns the Mail::Header object that holds the headers for the current message.
$mail->nntppost([options]) Posts an article via NNTP; requires Net::NNTP. Options are passed as key/value pairs. Available options are:
Prints the header, body, or whole message to file descriptor $mail->print(\*STDOUT); # Print message to STDOUT
$mail->remove_sig([nlines]) Removes a user's signature from the body of a message. Looks for a line equal to
$reply = $mail->reply( ) Creates a new object with headers initialized for a reply to the current object and with a body that is an indented copy of the current message.
$mail->smtpsend( ) Sends the Mail::Internet message via SMTP to all addresses on the In a future release of Mail::Internet,
$mail->tidy_body( ) Removes all leading and trailing lines that contain only whitespace from the message body.
$mail->unescape_from( ) Removes the escaping added by Parse Email Addresses with Mail::AddressMail::Address parses RFC 822-compliant mail addresses of the form: "Full Name or Phrase" <username@host> (Comment Area) For example, under RFC 822, an address might be represented as: "Nathan V. Patwardhan" <nvp@mail.somename.com> (No Comment) or: "Nathan V. Patwardhan" <nvp@mail.somename.com> The Mail::Address constructor parses an email address into three parts based on the categories shown above:
Mail::Address also outputs portions of the mail address with the functions print $addr->phrase( ); outputs: Nathan V. Patwardhan the print $addr->address( ); outputs: nvp@mail.somename.com and the print $addr->comment( ); outputs: No Comment A real mail address can be "unmangled," or parsed from its user@somehost.com format, with the print $addr->user; outputs: nvp And the following line using the print $addr->host; outputs: nvp@mail.somename.com Mail::Address Reference
$addr = Mail::Address->new(phrase, address[, comment]) Constructor. Creates new Mail::Address object representing an address with the specified elements. In a message, these three elements show up as: phrase <address> (comment) address (comment)
Mail::Address->parse(line) Constructor. Parses the specified line, usually a
$addr->address( ) Returns the address part of the object.
$addr->canon( ) Unimplemented, but should return the UUCP canon for the message.
$addr->comment( ) Returns the comment part of the object.
$addr->format( ) Returns a string representing the address in a form suitable for the
$addr->host( ) Returns the host portion of the address.
$addr->name( ) Takes information contained in the object and uses various heuristics to try to identify the name of the person or group.
$addr->path( ) Unimplemented, but should return the UUCP path for the message.
$addr->phrase( ) Returns the phrase part of the object.
$addr->user( ) Returns the user ID portion of the address. Checking Blacklisted Hosts with Mail::RBLMail::RBL eases the task of checking if a given host is in the real-time blackhole list. Mail::RBL does this by speaking directly to the RBL server and searching for a specified host. Ordinarily, you build this functionality into your MTA (sendmail, qmail, Exim, etc.), but if your system administrator hasn't built RBL support into your MTA, you might consider integrating Mail::RBL into your own mail filter.
Mail Filtering with Mail::AuditMail::Audit is a mail-filtering system for those who want to write good filter rules but choose not to use procmail. It was written to provide flexibility while realizing the strengths of Perl's regular expression engine. Inspired by Tom Christiansen's audit_mail and deliverdb programs, Mail::Audit allows an email message to be logged, examined, routed to another folder or INBOX, resent, or rejected. You should be able to write a simple ruleset or rulesets and put references to it in your forward file. For example:
Mail::Audit implements the following methods.
Constructor. Reads a mail message from STDIN (or, if the
If you specify a log level without a log file, logging will be written to /tmp/username-audit.log, where
body( ) Returns a reference to an array of lines in the body of the email.
get(header) Retrieves the named header from the mail message.
header( ) Returns the header as a single string.
Ignores an email message completely, dumping it into the bit bucket.
Opens a pipe to an external program and feeds the message to it.
put(header, value) Inserts a new header into the mail message with the given value.
Rejects an email message, then notifies the sender that the message is undeliverable with
resend(address) Bounces the email in its entirety to another address.
tidy( ) Tidies up the email as per the Mail::Internet manpage.
Writes the incoming (and filtered) mail into a mailbox at Alias Manipulation with Unix::AliasFileWhile not a member of the Mail umbrella, Unix::AliasFile is an important module since it implements a complete interface to the Unix aliases text file that allows you to query, add, update, and delete aliases. In addition, it has a cleaner interface than Mail::Alias. Unix::AliasFile automatically handles file locking, comma and colon placement, and any other detail that's related to manipulating your Unix mail alias file. For example:
Unix::AliasFile implements the following methods.
new(filename) Constructor. Creates a new Unix::AliasFile object.
add_user(alias, @users) Adds
alias(alias, @users) Adds, modifies, or returns information about an alias. When only
aliases( ) Returns an alphabetized list of all existing aliases. In scalar context, this method returns the total number of aliases.
comment(alias, comment) Inserts a comment line before alias.
commit( ) Writes the alias file.
delempty( ) Deletes all existing aliases that have no members, returning a count of the number of aliases deleted.
delete(alias) Deletes alias.
remove_user(alias, @users) Removes the list of users from an existing alias. If a user in
rename_user(oldname, newname) Changes one username to another in every alias. Returns the number of aliases affected.
uncomment(comment) Removes the comment from the file that is exactly matched to the supplied text. Returns on success and on failure. |