bobothesmart has asked for the wisdom of the Perl Monks concerning the following question:

Anyone know of any good tutorials on how to use Perl in a CGI script allow a user on a web site to fill out a form and have it be emailed to a specified address?

I'd like to do this without having to download any modules or anything - just using as basic a version of Perl as possible.

edited: Wed Jun 19 14:55:08 2002 by jeffa - changed title

  • Comment on Sending email from a CGI script (was: sendmail)

Replies are listed 'Best First'.
Re: sendmail
by DamnDirtyApe (Curate) on Jun 19, 2002 at 14:29 UTC
Re: sendmail
by emilford (Friar) on Jun 19, 2002 at 14:48 UTC
    If you're just looking for a way to get the input from the form, I'd have to suggest using the CGI.pm module. The use of modules is stressed here for good reason and I believe the CGI.pm module is fairly standard. Then you could just do something like
    use CGI; $fav_color = param("favorite_color");
    to get the variable input. I'd consult OVID's tutorial mentioned above for the exact usage of the CGI module.

    If you really don't want to use any modules, the following code will create a hash called formdata which contains the form input as name/value pairs.
    sub parse_form { my (@pairs, $buffer, $value, $key, $pair); if ($ENV{'REQUEST_METHOD'} eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } else { print "Content-type: text/html\n\n"; print "<P>Use Post or Get"; } foreach $pair (@pairs) { ($key, $value) = split (/=/, $pair); $key =~ tr/+/ /; $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~s/<!--(.|\n)*-->//g; if ($formdata{$key}) { $formdata{$key} .= ", $value"; } else { $formdata{$key} = $value; } } return %formdata; }
    You'd then call the subroutine with something like my %formdata = &parse_form

    After you get the form information, using either of the above mentioned methods, you can use the sendmail program, which is standard on most any *NIX box.
    open (MAIL, "|/usr/bin/sendmail -t") or die "Error"; print MAIL "To: to@address.com \nFrom: from@address.com\n"; print MAIL "Subject: mail subject\n"; print MAIL "Add the contents here\n"; close (MAIL);
    Hope this helps. -Eric
Re: Sending email from a CGI script
by Aristotle (Chancellor) on Jun 19, 2002 at 19:41 UTC
    You might want to have a look at Net::SMTP, which comes with the Perl distribution.

    Makeshifts last the longest.