18.3. Sending MailProblemYou want your program to send mail. Some programs monitor system resources like disk space and notify appropriate people by mail when disk space becomes dangerously low. CGI script authors may not want their programs to report errors like "the database is down" to the user, preferring instead to send mail to the database administrator notifying them of the problem. SolutionUse the CPAN module Mail::Mailer:
use Mail::Mailer;
$mailer = Mail::Mailer->new("sendmail");
$mailer->open({ From => $from_address,
To => $to_address,
Subject => $subject,
})
or die "Can't open: $!\n";
print $mailer $body;
$mailer->
Or, use the open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") or die "Can't fork for sendmail: $!\n"; print SENDMAIL <<"EOF"; From: User Originating Mail <me\@host> To: Final Destination <you\@otherhost> Subject: A relevant subject line Body of the message goes here, in as many lines as you like. EOF close(SENDMAIL) or warn "sendmail didn't close nicely"; DiscussionYou have three choices for sending mail from your program. You can use another program that users normally use to send mail, like Mail or mailx ; these are called MUAs or Mail User Agents . You can use a system-level mail program like sendmail ; this is an MTA, or Mail Transport Agent . Or you can connect to an SMTP (Simple Mail Transfer Protocol) server. Unfortunately, there's no standard user-level mail program, sendmail doesn't have a standard location, and SMTP isn't particularly simple. The CPAN module Mail::Mailer hides these complexities from you. When Mail::Mailer is installed, it looks for mail , Mail , and other names mail-sending programs tend to hide under. It also looks in common locations for sendmail . When you create a Mail::Mailer object, you get convenient access to those programs (and SMTP mail servers) without needing to know their argument structure or how they return errors.
Create a Mail::Mailer object with For instance, here is how to instruct Mail::Mailer to use sendmail instead of its default: $mailer = Mail::Mailer->new("sendmail");
Here's how to tell it to use $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, 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 $mailer->open( 'From' => 'Nathan Torkington <gnat@frii.com>', 'To' => 'Tom Christiansen <tchrist@perl.com>', 'Subject' => 'The Perl Cookbook' );
The 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($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: Tom Christiansen <tchrist\@perl.com> To: Nathan Torkington <gnat\@frii.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. tom EOF close(SENDMAIL);
This is a straightforward use of
We Some ports of Perl (Windows and Mac particularly) don't have sendmail or mail to use. In these cases, you should find an SMTP server you can send mail through. See Also
The |
|