Create a Mail::Mailer object with
Mail::Mailer->new. If you don't pass any
arguments, it uses the default mail sending method (probably a
program like mail). Arguments to
new select an alternate way to send the message.
The first argument is the type of delivery method
("mail" for a Unix mail user agent,
"sendmail" for sendmail, and
"smtp" to connect to an SMTP server). The optional
second argument is that program's path.
For instance, to instruct Mail::Mailer to use
sendmail instead of its default:
$mailer = Mail::Mailer->new("sendmail");
Here's how to tell it to use
/u/gnat/bin/funkymailer instead of
mail:
$mailer = Mail::Mailer->new("mail", "/u/gnat/bin/funkymailer");
Here's how to use SMTP with the machine
mail.myisp.com as the mail server:
$mailer = Mail::Mailer->new("smtp", "mail.myisp.com");
If an error occurs at any part of Mail::Mailer,
die is called. To check for these exceptions, wrap
your mail-sending code in eval and check
$@ afterward:
eval {
$mailer = Mail::Mailer->new("bogus", "arguments");
# ...
};
if ($@) {
# the eval failed
print "Couldn't send mail: $@\n";
} else {
# the eval succeeded
print "The authorities have been notified.\n";
}
The new constructor raises an exception if you
provide arguments it doesn't understand, or if you specify no
arguments and it doesn't have a default method. Mail::Mailer won't
run a program or connect to the SMTP server until you call the
open method with the message headers:
$mailer->open( { From => 'Nathan Torkington <gnat@frii.com>',
To => 'Tom Christiansen <tchrist@perl.com>',
Subject => 'The Perl Cookbook' } );
The open method raises an exception if the program
or server couldn't be opened. If open succeeds,
you may treat $mailer as a filehandle and print
the body of your message to it:
print $mailer << EO_SIG;
Are we ever going to finish this book?
My wife is threatening to leave me.
She says I love EMACS more than I love her.
Do you have a recipe that can help me?
Nat
EO_SIG
When you're done, call the close function on the
Mail::Mailer object:
close($mailer) or die "can't close mailer: $!";
If you want to go it alone and communicate with
sendmail directly, use something like this:
open(SENDMAIL, "|/usr/sbin/sendmail -oi -t -odq")
or die "Can't fork for sendmail: $!\n";
print SENDMAIL << "EOF";
From: Fancy Chef <chef@example.com>
To: Grubby Kitchenhand <hand@example.com>
Subject: Re: The Perl Cookbook
(1) We will never finish the book.
(2) No man who uses EMACS is deserving of love.
(3) I recommend coq au vi.
Frank Wah
EOF
close(SENDMAIL);