home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


18.3. Sending Mail

Problem

You 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.

Solution

Use 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->
close();

Or, use the sendmail program directly:

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";

Discussion

You 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 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 let you pick an alternative way of sending the message. The first argument is the type of delivery method ( "mail" for a Unix mail user agent, "sendmail" for sendmail, and "smtp" to open a connection to an SMTP server). The optional second argument is the path to the program.

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 /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. This means if you want to check for errors, you need to 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 headers of the message:

$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: 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 open to run another program (see Recipe 16.4 ). You need to specify the full path to sendmail because its location varies from machine to machine. It is often found in places like /usr/lib or /usr/sbin . The flags we give to sendmail say to not exit when a line with only a dot is read ( -oi ), to read the headers of the message to decide whom to send it to ( -t ), and to insert the message into the queue instead of attempting to deliver it immediately ( -odq ). This last option is only important when you're sending a lot of mail  - omitting it would quickly swamp the machine with sendmail processes. If you want immediate delivery of your message (for instance, you're testing your program or the mail is urgent) remove -odq from the command line.

We print an entire message, headers and then body, separated by a blank line. There are no special escapes to insert new lines (as some user mail programs have), so all text is literal. sendmail adds headers like Date and Message-ID (which you shouldn't generate yourself anyway).

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 open function in Chapter 3 of Programming Perl and in perlfunc (1); Recipe 16.4 ; Recipe 16.10 ; Recipe 16.19 ; Recipe 19.6 ; the RFCs dictating the SMTP protocol, RFC 821, Simple Mail Transfer Protocol , as amended by later RFCs


Previous: 18.2. Being an FTP Client Perl Cookbook Next: 18.4. Reading and Posting Usenet News Messages
18.2. Being an FTP Client Book Index 18.4. Reading and Posting Usenet News Messages

Library Navigation Links

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