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

Hi, Trying to route a form based on selected fields. If a user selects a particular field I want the form emailed to one particular user. Here is some of my code:
#!/usr/local/bin/perl -- -*-perl-*- # ------------------------------------------------------------ # check-in.cgi # Interactive form for Human Resources Office # # ------------------------------------------------------------ # Define mail program $mailprog = '/usr/lib/sendmail'; # Recipient of form responses $recipient = 'yogibear@test.edu'; # Print out a content-type for HTTP/1.0 compatibility print "Content-type: text/html\n\n"; # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # Stop people from using subshells to execute commands # Not a big deal when using sendmail, but very important # when using UCB mail (aka mailx). # $value =~ s/~!/ ~!/g; # Uncomment for debugging purposes # print "Setting $name to $value<P>"; $FORM{$name} = $value; } # Print title and header info for user response &user_response; $date = `date`; # Now send mail to $recipient formatted in HTML # Note: MIME, Subject and Content lines must appear EXACTLY as they do + below # (only one \n after Subject line!) open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n"; print MAIL "Reply-to: $FORM{'espr2'} $FORM{'estb'} $FORM{'espr'}\n"; print MAIL "MIME-Version: 1.0\n"; print MAIL "To: $recipient\n"; print MAIL "From: $FORM{'espr2'} $FORM{'estb'} $FORM{'espr'}\n"; print MAIL "Subject: ES Internal Routing Form> $FORM{'name'}: $FORM{'i +d'} \n"; print MAIL "Content-Type: text/html; charset=us-ascii\n"; print MAIL "Content-Transfer-Encoding: 7bit\n"; print MAIL "<html><head>\n"; print MAIL "<title>Human Resources Office: "; print MAIL "ES Internal Routing Form</title>\n"; print MAIL "</head>\n"; print MAIL "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#003366\ +" "; print MAIL "alink=\"#666666\" vlink=\"#6699CC\">\n"; print MAIL "<center><table border=0 width=99%>"; print MAIL "<tr><td align=left>"; print MAIL "<img src=\"/graphics/smdbw_logo.gif\" "; print MAIL "alt=\"logo\"></td>"; print MAIL "<td align=center><font color=\"#003366\" size=+2>"; print MAIL "Human Resources Office<br>"; print MAIL "Internal Routing Form</font></td></tr>"; print MAIL "</table>"; print MAIL "<img src=\"/graphics/2lines.gif\" "; print MAIL "alt=\"2lines\"></center>"; print MAIL "<font color=\"#FF0000\"><b>Processed By: </b>$FORM{'espr2' +} $FORM{'estb'} $FORM{'espr'}</font><br>"; print MAIL "<b>Name: </b>$FORM{'name'}<br>"; print MAIL "<b>SS#: </b>$FORM{'ss'}<br>"; print MAIL "<b>ID#: </b>$FORM{'id'}<br>"; print MAIL "<b>Dept: </b>$FORM{'deptnumber'}<br>"; print MAIL "<b>Title: </b>$FORM{'title'}<br>"; print MAIL "<b>DOB: </b>$FORM{'dob'}<br>"; print MAIL "<font color=\"#FF0000\"><b>US Citizen? </b>$FORM{'ckusyes' +} $FORM{'ckusno'}</font><br>"; print MAIL "<b>If not, which country? </b>$FORM{'country'}<br>"; print MAIL "<b>Visa Type: </b>$FORM{'visa'}<br>"; print MAIL "<b>EEO Designation </b>$FORM{'eeodesgination'}<br>"; print MAIL "<b>Veteran: </b>$FORM{'eeoveteran'}<br>"; print MAIL "<b>Disabled: </b>$FORM{'eeodisabled'}<br>"; print MAIL "<hr>"; print MAIL "<font color=\"#003366\" size=+2><b>Employment Action </b>< +/font><br>"; print MAIL "<hr>"; print MAIL "<b>Action: </b>$FORM{'newhire'} $FORM{'rehire'} $FORM{'ter +m'} $FORM{'LOA'} $FORM{'change'} $FORM{'promotion'} $FORM{'extension' +}<br>"; print MAIL "<b>Dept Transfer To: </b>$FORM{'depttransfer'}<br>"; print MAIL "<b>Effective Date: </b>$FORM{'effectivedate'}<br>"; print MAIL "<b>Expiration Date: </b>$FORM{'expirationdate'}<br>"; print MAIL "<b>Termination Date: </b>$FORM{'terminationdate'}<br>"; print MAIL "<b>Termination Reason: </b>$FORM{'termreason'}<br>"; print MAIL "<hr>"; print MAIL "<b>Type/Status: </b>$FORM{'ft'} $FORM{'threequarter'} $FOR +M{'half'} $FORM{'casual'} $FORM{'education'} $FORM{'guest'}<br>"; print MAIL "<b>Reg/Temp: </b>$FORM{'regular'} $FORM{'temporary'}<br>"; print MAIL "<b>Graded/Exempt: </b>$FORM{'graded'} $FORM{'exempt'}<br>" +; print MAIL "<hr>"; print MAIL "<b>Annual Salary: </b>$FORM{'annsalary'}<br>"; print MAIL "<b>TRE: </b>$FORM{'tre'}<br>"; print MAIL "<b>Supervisor: </b>$FORM{'super'}<br>"; print MAIL "<b>Office Address: </b>$FORM{'whoiadd'}<br>"; print MAIL "<b>Mail Stop: </b>$FORM{'ms'}<br>"; print MAIL "<b>Extension: </b>$FORM{'extension'}<br>"; print MAIL "<hr>"; print MAIL "<b>Primary Address: </b>$FORM{'primaryadd'}<br>"; print MAIL "<b>Primary Phone: </b>$FORM{'primaryphone'}<br>"; print MAIL "<b>Publish in the WHOI Directory? </b>"; print MAIL "$FORM{'pubaddry'} $FORM{'pubaddrn'}<br>"; print MAIL "<hr>"; print MAIL "<b>New Position: </b>$FORM{'newposition'}<br>"; print MAIL "<b>Job Code: </b>$FORM{'jobcode'}<br>"; print MAIL "<b>EEO Code: </b>$FORM{'eeocode'}<br>"; print MAIL "<b>Market Average: </b>$FORM{'marketave'}<br>"; print MAIL "<b>Additional Comments: </b>$FORM{'comments'}<br>"; print MAIL "<hr>"; print MAIL "<b>Emergency Contact: </b>$FORM{'emername'}<br>"; print MAIL "<b>Relationship: </b>$FORM{'emerrelate'}<br>"; print MAIL "<b>Emergency Address: </b>$FORM{'emeradd'}<br>"; print MAIL "<b>Emergency Phone: </b>$FORM{'emerphone'}<br>"; print MAIL "<hr>"; print MAIL "</body></html>"; close (MAIL);

Replies are listed 'Best First'.
Re: form recipients
by Roy Johnson (Monsignor) on Mar 10, 2004 at 18:36 UTC
    Is there a reason you're not using the CGI and maybe the Mime::Lite modules?

    What is your actual question? You don't seem to have an if checking to see whether the mail should actually be sent. If the problem is that it sends the mail every time, instead of only when you want it to, that's probably the reason.


    The PerlMonk tr/// Advocate
Re: form recipients
by arden (Curate) on Mar 10, 2004 at 18:37 UTC
    Ok, cool. What's your question? If you're getting an error, what's the error? To change who the email gets sent to, set up some logic before the  open(MAIL ... line where
    # psuedo code here if ( $FORM{'thisbox'} ) { $recipient = 'fozzybear@muppets.edu'; else if ( $FORM{'differentbox'} ) { $recipient = $recipient.',bigbird@sesame.edu'; }
    but now I'm trying to guess what your problem is and that's not much fun. . .

    - - arden.

    update: I forgot to ask, which version of Perl do you have on your system?

      Thank you first off, Secondly, I think my problem may be were I am putting these lines. I don't have any success. The particular field I need is the  print MAIL "<font color=\"#FF0000\"><b>US Citizen? </b>$FORM{'ckusyes'} $FORM{'ckusno'}</font><br>"; If the 'chusno' field is selected then either add a recipient or change the recipients all together.
        That is too late. You need to check  $FORM{'ckusno'} BEFORE you  open(MAIL ... and alter the value of $recipients then. You can still re-use the variable in the body of the email message, but you need to know who to send the message to before you start to send the message.

        - - arden.