MailTool - Handy-dandy MIME mailing class


 use lib some_directory, depending on where you put the .pm files
 use MailTool;
 $msg = MailTool->build (
        To => '',
        From => '',
        Subject => 'The check is in the mail',
        Path => standard_reply.txt,
 $msg->send ();


The MailTool class is actually a subclass of MIME::Lite. It provides no new methods, but does 'enhance' (I hope!) some of the existing ones. The natures of the enhancements (if enhancements they be) are:

The default send method is now smtp.

This may not be an enhancement if you're running Unix. The default of MIME::Lite is (or was when I wrote this) to spawn SENDMAIL.

The SMTP host can be specified any number of ways.

If you are using the send method, and are in fact using SMTP (i.e. you have not used the class send method to change the default way to send mail), the SMTP host comes from the first thing in the following list that is actually defined:

 The second argument of the class send method;
 The contents of environment variable SMTPHOSTS;
 The contents of smtp_hosts in Net::Config;
 The name of the host that is executing the script.

If you are calling send_by_smtp explicitly, the information specified to the class send method is ignored (because this is consistent with the behaviour of MIME::Lite), and the SMTP host is determined by the first thing in the following list that is actually defined:

 The first argument passed to send_by_smtp;
 The contents of environment variable SMTPHOSTS;
 The contents of smtp_hosts in Net::Config;
 The name of the host that is executing the script.

Multiple SMTP hosts can be specified, either by passing a reference, or by separating the names by colons (the normal way of specifying SMTPHOSTS, which is used by Mail::Internet). If you specify multiple hosts, they are tried in the order specified until the send succeeds or the list of hosts is exhausted. In the latter case, an exception is raised which describes the last error encountered.

Specifying multiple hosts while using a send method other than smtp is unsupported.

Mailing lists are supported.

Any address element that begins with an "@" sign is assumed to be a reference to a mailing list, with the rest of the address element being the name of the file. This file is opened and read, and any and all lines in the file are appended to the address list. This functionality works for the "To", "Cc", and "Bcc" tags only.

You get an error message back if send_by_smtp fails.

This message is the first error encountered in the attempt to send using the last host on the list.


The following Perl modules are required: Carp (standard module) FileHandle (standard module) Mail::Address (from library MailTools) MIME::Lite (from library MIME-lite) Net::Config (from library libnet) Net::SMTP (from library libnet) Sys::Hostname (standard module)

Note that these in turn can have requirements of their own. What these requirements are is best found by reading the documentation for the libraries themselves, but you can pretty much count on needing at least MIME::Base64 (from MIME-base64). On the other hand, the only known new nonstandard requirement (over and above those of MIME::Lite) are Mail::Address (because I was lazier than Eryq, and didn't provide a hack to cover its absence). Truth to tell, Net::Config is also new, but if you have Net::SMTP you should have Net::Config as well.

$msg->add (tag, value)

This override of MIME::Light::add is the hook where mailing list functionality is provided.

It looks up the tag being added in its internal hash table. If a hit is found, both tag and value are passed to the subroutine specified in the hash table, and the value returned is passed to SUPER::add. If no hit is found, the pristine value is passed to SUPER::add.

In the case of the 'to', 'cc', and 'bcc' tags, the effect is to try to expand all addresses beginning with "@" as mailing lists. No other tags are currently munged.

$msg->send_by_smtp ([smtp_host])

This override of MIME::Lite's send_by_smtp method does pretty much the same thing (it should! the code was stolen shamelessly! Thanks, Eryq!) It has, however, the following differences:

 * More error detail (sometimes)
 * Sensitivity to a number of sources of SMTP server information:
    - Explicitly in the argument(s) to the method;
    - From environment variable SMTPHOSTS (colon-separated list);
    - From Net::Config;
    - If all else fails, use the local machine.


Copyright 2001 by E. I. DuPont de Nemours and Company. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

This software comes with no warranty whatsoever, either expressed or implied.


Tom Wyant (, E. I. DuPont de Nemours and Company, Inc. (, and no, you won't find anything about this module there).