start page | rating of books | rating of authors | reviews | copyrights

Book HomeCGI Programming with PerlSearch this book

9.6. Perl Mailers

There are other programs you can use for sending mail, but they are not as common. Some of these, such as blat, provide simple mailers for Windows systems. Instead of looking at these, we'll look at a Perl solution that works across all operating systems.

Mail::Mailer is a popular Perl module for sending Internet email. It provides a simple interface for sending messages with sendmail and mail (or mailx). It also allows you to send messages via SMTP without an external application, which makes it possible to send messages on non-Unix systems like Windows and even the MacOS.

You can use Mail::Mailer this way:

my $mailer = new Mail::Mailer ( "smtp" );
$mailer->open( {
    To       => $email,
    From     => 'The Webmaster <[email protected]>',
    Subject  => 'Web Site Feedback'
} );

print $mailer <<END_OF_MESSAGE;
Your message has been sent and someone should be responding to you 
shortly. Thanks for taking the time to provide us with your feedback!
END_OF_MESSAGE

close $mailer;

When you create a Mail::Mailer object, you can specify whether you want it to send the message one of three ways:

mail

Mail::Mailer will search your system for mailx, Mail, or mail in that order and use the first one it finds (we didn't discuss Mail, although on many systems Mail and mail are the same -- mail is simply a symlink to Mail ).

sendmail

Mail::Mailer will use sendmail to send mail.

smtp

Mail::Mailer will use the Net::SMTP Perl module to send mail.

If you do not specify an argument when you create an object, Mail::Mailer will search through each of these three options in order and use the first one it finds When Mail::Mailer uses an external mailer, it uses the fork and exec technique to avoid passing arguments through the shell.

Mail::Mailer is primarily useful when you use it to send mail via SMTP on systems without sendmail. Even though it allows you to use sendmail as its mailer, there is no way for you to specify command-line options the way you can if you use sendmail directly. Mail::Mailer only uses the -t option when it calls sendmail.

To send mail directly through SMTP with Mail::Mailer, you need to have the Net::SMTP module, which is part of the libnet bundle available on CPAN. When you install this module, it should ask you for the SMTP server you use on your network. If this was not configured when the module was installed, you have two options. You can edit the installed Net/Config.pm file in your Perl libraries folder and add your SMTP server to the smtp_hosts element of the NetConfig hash at the bottom of the file, or you can specify it when you create a Mail::Mailer object. You can do so like this:

my $mailer = new Mail::Mailer ( "smtp", Server => $server );

In this example, $server would contain the name of your SMTP server. Your network administrator or internet service provider should be able to provide you with the name of this machine.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.