A Mailing Function

The objective of the following sections is for you to write a short Perl function that you can use in your CGI programs to send mail. There is a problem, though. The way the function works is fairly dependent on whether you have a local MTA (such as sendmail) or whether you have to send the mail yourself to an SMTP relay host. So scan ahead, and decide which of the following sections you're going to need for your particular program.

A Mail Function for UNIX Systems

If you have a UNIX system, and sendmail is properly configured (it probably will be), you're reading the right section. If you don't have UNIX or sendmail, and you're reading this section out of curiosity—good for you. However, the function presented in Listing 22.1 probably won't do you much good.

Note

Even if you have a UNIX system, the next section, "A Mail Function for Non-UNIX Systems," might be worth reading. A new technique for using a module—an object-oriented module—is discussed there.


Listing 22.1 Function send_mail
1:   # Function for sending mail with an MTA like sendmail
2:   sub send_mail {
3:       my($to, $from, $subject, @body)=@_;
4:
5:       # Change this as necessary for your system
6:       my $sendmail="/usr/lib/sendmail -t -oi -odq";
7:
8:       open(MAIL, "|$sendmail") || die "Can't start sendmail: $!";
9:       print MAIL<<END_OF_HEADER;
10:  From: $from
11:  To: $to
12:  Subject: $subject
13:
14:  END_OF_HEADER
15:      foreach (@body) {
16:              print MAIL "$_
";
17:      }
18:      close(MAIL);
19:  }

Line 6: The location of sendmail and the arguments that it needs are put into a variable here. The sendmail program may be in a different location on your system or may take different arguments.

Line 8: The sendmail program specified in $sendmail is started and opened for writing on the filehandle MAIL.

Lines 9–14: The header of the mail message is written to MAIL.

Lines 15–17: The body of the message is written to the MAIL filehandle. Each line has a appended to it.

To use this function, simply call it with four arguments like this:

@body=("Lower mine, please.", "Thanks!");
send_mail('[email protected]', "[email protected]',
    'Taxes', @body);

This function relies on sendmail being properly installed and configured on your system. If it's not, see the next section, "A Mail Function for Non-UNIX Systems;" the solution presented there should work under UNIX as well.

You need to change the variable $sendmail to the correct location of the sendmail program on your system. It's most commonly in /usr/lib, although it can be in /usr/sbin, /lib, or any other directory on your system. You might need to hunt around for it.

Note

If things don't work as you expect, make sure that mail is configured properly on your system. Use a mail utility such as mail or pine to try sending a test mail message. If those utilities don't work properly, it's very unlikely that sendmail is set up properly. You need to fix that problem first or use the approach listed in the next section.


In Listing 22.1, the sendmail program is started with the following options; you can change them as you see fit.

-tTakes header fields (From, To, Subject, and so on) from the input instead of the command line.
-oiIgnores . (a period) on a single line by itself. Not using this option could prematurely terminate your email message.
-odqQueues messages instead of trying to send them immediately. You can leave off this option if you like. If too many messages are sent immediately, however, your mail system could be swamped with requests. Using -odq is being polite.

The rest of the function send_mail() should be self-explanatory.

A Mail Function for Non-UNIX Systems

Under Windows and other operating systems that do not have a built-in MTA such as sendmail, you run into complications. MTAs are not simple beasts, and trying to duplicate what they do with a few lines of Perl isn't easy. But it can be done.

First, the good news: The Perl module Net::SMTP allows you to send mail from any kind of operating system that Perl runs on. Using this module, you can easily send the mail without much hassle.

Now for the bad news: This module isn't installed with the standard Perl distribution. To get it, you need to load it onto your system where the Web server is—or wherever you're trying to send mail from. The Net::SMTP module is part of the libnet bundle, which contains all kinds of useful networking modules. The libnet bundle is on the CD-ROM included with this book.

Note

The Appendix, "Installing Modules," has a fairly detailed how-to guide on installing Perl modules. It explains how to install modules under UNIX, Windows, and Macintosh operating systems. In addition, you'll find instructions on how to install your own private copies of modules if your system administrator will not install a public copy.


The send_mail function for systems without an MTA is shown in Listing 22.2. It contains some rather strange, new syntax that you're not familiar with. Be sure to read the explanation afterward.

Listing 22.2 Function send_mail for Non-MTA Systems
1: # Function for sending mail for systems without an MTA
2: sub send_mail {
3:      my($to, $from, $subject, @body)=@_;
4:
5:      use Net::SMTP;
6:
7:      # You will need to change the following line
8:      # to your mail relay host
9:      my $relay="relayhost.yourisp.com";
10:     my $smtp = Net::SMTP->new($relay);
11:     die "Could not open connection: $!" if (! defined $smtp);
12:
13:     $smtp->mail($from);
14:     $smtp->to($to);
15:
16:     $smtp->data();
17:     $smtp->datasend("To: $to
");
18:     $smtp->datasend("From: $from
);
19:     $smtp->datasend("Subject: $subject
");
20:     $smtp->datasend("
");
21:     foreach(@body) {
22:          $smtp->datasend("$_
");
23:     }
24:     $smtp->dataend(); # Note the spelling: no "s"
25:     $smtp->quit;
26: }

Line 5: The Net::SMTP module is brought in to make sending mail a little easier.

Line 10: A Net::SMTP object is created, connected to the correct relay host, which you set on line 9.

Lines 13–23: The headers and body for the e-mail are sent to the relay host. See the explanation of the Net::SMTP functions that follow for more details.

To use this function, simply call it with four arguments representing the pieces of the email message:

@body=("Lower mine, please.", "Thanks!");
send_mail('[email protected]', '[email protected]',
    'Taxes', @body);

The first thing that should strike you odd about this function is the line $smtp = Net::SMTP->new($relay);. This line creates something called an object. An object isn't really a scalar, a hash, or an array; it's something a little different. The value in $smtp now represents a connection to a mail program, which you can manipulate. Think of it as a special kind of value that allows you to call functions that are related to it.

The next thing that should strike you as odd is the line $smtp->mail($from);. The -> connects an object to a function called against it. So mail is a function call using the $smtp object that was created on the previous line.

You don't really need to understand everything about object syntax to use the Net::SMTP module—just enough to get by. The functions that you can use against a Net::SMTP object are as follows:

  • $smtp->mail(addr)—The mail function indicates who you're sending mail as. Yes, you can lie about who you are sometimes.

  • $smtp->to(addr)—The to function indicates to whom you're sending mail. If you call to with a list of names, each person receives a copy of the mail. The names of these people don't necessarily appear in the body of the message, unless you explicitly put them there yourself—for example, to send BCCs.

  • $smtp->data();—The data function indicates that you're ready to send the message itself.

  • $smtp->datasend(data)—This function sends the actual text of the message. You must print your own header fields (To:, From:, and so on). Header fields such as Date: and Received: are generated automatically. Between the header and the body, you must also print a blank line—$smtp->datasend(" "). The body of your message follows the blank line and is also sent with $smtp->datasend().

  • $smtp->dataend()—The dataend function indicates that you're done with the message body; the message isn't sent until this happens.

  • $smtp->quit()—This function disconnects from the SMTP server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.204.201