Example: Sending Email from a CGI Script

Let’s look at a simple example that illustrates how email is sent using a CGI script. I’ve written a CGI script (email-form.pl) that presents users with a form that requests their email address, a subject, and a message body. When the user fills out the form fields and submits the form, an email message with the subject and body that they entered is sent to the email address specified in the script. Theoretically, the email address is their own, but that’s not necessary. The full source code for the script appears in Listing 8.2.

Listing 8.2. The Source Code for email-form.pl
 1: #!/usr/local/bin/perl 2: 3: use CGI; 4: use Net::SMTP; 5: 6: my $query = new CGI; 7: my $smtp = Net::SMTP->new('localhost'); ...

Get Sams Teach Yourself CGI in 24 Hours, Second 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.