Email

From time to time a CGI script needs to send someone an email. If it’s via a link selected by the user, use the HTML construct:

<A HREF="mailto:administrator@butterthlies.com">Click here to email the
        administrator</A>

The user’s normal email system will start up, with the address inserted.

If you want an email to be sent automatically, without the client’s collaboration or even her knowledge, then use the Unix sendmail program (see man sendmail). To call it from Perl (A is an arbitrary filename):

open A, "| sendmail -t" or die "couldn't open sendmail pipe $!";

A Win32 equivalent to sendmail seems to be at http://pages.infinit.net/che/blat/blat_f.html. However, the pages are in French. To download, click on “ici” in the line:

Une version récente est ici.

Alternatively, and possibly safer to use, there is the CPAN Mail::Mailer module.

The format of an email is pretty well what you see when you compose one via Netscape or MSIE: addressee, copies, subject, and message appear on separate lines; they are written separated by \n. You would put the message into a Perl variable like this:

$msg=qq(To:fred@hissite.com\nCC:bill@elsewhere.com\nSubject:party tonight\n\nBe at 
Jane's by 8.00\n);

Notice the double \n at the end of the email header. When the message is all set up, it reads:

print A $msg 
close A or die "couldn't send email $!";

and away it goes.

Get Apache: The Definitive Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.