CIS 24: CGI and Perl Programming for the Web

Class 13 (12/11) Lecture Notes

Topics

  1. Server Side Includes
  2. Using Email
  3. Lab

Return to CIS 24 home page


  1. Server Side Includes
  2. We can use the excellent Server Side Includes (SSI) tutorial at the Big Nose Bird site to learn about SSI.

    Also useful is the official Apache documentation on Server Side Includes: Apache Module mod_include

  3. Using Email
  4. Just like web pages use a protocol for communication (HTTP - the Hypertext Transfer Protocol), the transfer of email across the Internet relies on its own protocol - the Simple Mail Transport Protocol. To send email from your Perl scripts, you'll use the Net::SMTP module - let's take a look at the article Perl: How to Send Email at about.com

    As mentioned in the tutorial, Net::SMTP is part of the libnet package. You can download a version of it that's already designed to run with the ActiveState version of Perl by using the Perl Package Manager.

    Here's a sample script that uses the Net::SMTP module:

    #!perl
    use Net::SMTP;
        
    $smtp = Net::SMTP->new('smtp.best.com', debug => 1);
        
    $smtp->mail('toppa@ask.com'); # the sender's address
    $smtp->to('mike@toppa.com'); # the recipient's address
        
    $smtp->data();
    
    # The next three lines are the email headers,
    # which are analagous to the HTTP headers for web pages
    
    $smtp->datasend("To: mike\@toppa.com\n");
    $smtp->datasend("From: toppa\@ask.com\n");
    
    # if you're using CGI, the reply address won't be the
    # same as the "from" address - you need to set it as well.
    
    $smtp->datasend("Reply-to: toppa\@ask.com\n");
    
    # as in HTTP, two linefeeds separate the headers from the body
    
    $smtp->datasend("\n");
    
    # we'll use just a single-line message for the body
    
    $smtp->datasend("A simple test message\n"); 
    $smtp->dataend();
        
    $smtp->quit;

    Unix systems have a built-in application that you can use to send message via SMTP - it's called sendmail. Here's a sample script:

    #!/usr/bin/perl
    unless (open (MAIL,"|/usr/sbin/sendmail -t")) {
    	print "unable to open sendmail";
    	exit;
    }
    
    print MAIL "To: mike\@toppa.com\n";
    print MAIL "From: toppa\@best.com\n";
    print MAIL "Reply-To: toppa\@best.com\n";
    print MAIL "Subject: test message\n\n";
    
    print MAIL "This is a test email message\n";
    close (MAIL);

  5. Lab
  6. Work on your projects - they're due on 12/18, which is next week!

Return to CIS 24 home page