Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

passing CGI form parameters via HTML email

by gmacfadden (Sexton)
on Jun 21, 2006 at 19:40 UTC ( [id://556755]=perlquestion: print w/replies, xml ) Need Help??

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

Despite researching this via Perldoc, the web, and my Perl texts over several days, I remain stumped. My simple objective is to successfully execute the CGI script identified by the form action="...." parameter when the form is embedded in an email (in lieu of the more usual HTML file. I OBSERVE THAT THE FOLLOWING HTML FILE:
<html><head><title>Form data originating from HTML</title></head> <body> <form action="http://www.gmacfadden.com/cgi-bin/view_sent_form_data.cg +i" method="POST"> Your Subject: <input type="text" name="subject"><br> Your Comments: <textarea name="comments" rows="2" cols="15"></textarea +><br> <input type="submit" value="Send"> </form> </body> </html>
WORKS PERFECTLY WITH THE FOLLOWING PERL SCRIPT (LET'S CALL IT PERL SCRIPT #1)located at http://www.gmacfadden.com/cgi-bin/view_sent_form_data.cgi:
#!/usr/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header; print start_html("View data from form executed in HTML email"); my $subject = param("subject"); my $comments = param("comments"); print "\$subject is $subject<br>\n"; print "\$content is $comments<br>\n"; print end_html;
THE CRUX OF MY QUESTION IS WHY DOES PERL SCRIPT #1 FAIL TO PROVIDE THE SAME EXPECTED RESULTS WHEN I EXECUTE THE FORM FROM WITHIN AN EMAIL (USING THE SAME INPUT DATA AND THE SAME EMBEDDED FORM AS IN THE HTML FILE)AS CREATED USING THE FOLLOWING PERL SCRIPT (LET'S CALL IT PERL SCRIPT #2)?
#!/usr/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; my $torecipients = "gmacfadden\@aol.com"; #substitur your email addre +ss here my $header1 = qq (Mime-Version:1.0); my $header2 = qq (Content-type: text/html; charset="iso-8859-1"); my $content = qq( <html><body><center> <form enctype="multipart/form-data" action="http://www.gmacfadden.com/ +cgi-bin//view_sent_form_data.cgi" method="POST"><br> Subject: <input type="text" name="subject"><br> Comments: <textarea name="content" rows="2" cols="15"></textarea><br> <input type="submit" value="Send"> </form></center></body></html> ); print header; print start_html("Create form within HTML E-Mail"); $ENV{PATH} = "/usr/sbin"; open (MAIL, "|/usr/sbin/sendmail -oi -t") or &dienice("Can't fork for sendmail: $!\n"); print MAIL "$header1\n"; print MAIL "$header2\n"; print MAIL "To: $torecipients\n"; print MAIL "From: anybody\@anywhere.org\n"; print MAIL "Subject: Please complete the enclosed form\n\n"; print MAIL "$content\n"; close(MAIL); print <<EndHTML; <h2>The requested form data has been embedded in an email sent to you! + Thank you</h2> EndHTML print end_html; sub dienice # The dienice subroutine handles errors. { my($errmsg) = @_; print "<h2>Error</h2>\n"; print "<p>$errmsg</p>\n"; print end_html; exit; }

Replies are listed 'Best First'.
Re: passing CGI form parameters via HTML email
by samtregar (Abbot) on Jun 21, 2006 at 20:21 UTC
    PLEASE STOP SHOUTING AT US!

    Ahem. You failed to mention what happens when you try to use the form in an email client. Does it do nothing? Does it produce an error?

    My guess is that the email client you're using is preventing the form from working as a security measure. Try testing it with a variety of clients.

    -sam

      Sorry, I did not realize anything in all caps was unwelcome form. Please forgive me. I shall respond to your questions in order.

      When I invoke CGI script 1 via server side HTML, I get the desired and expected output: e.g.
      $subject is: {input from HTML form}
      $comments is: {other input from HTML form}
      ......in other words, everything works fine.

      When I invoke CGI script 1 via responding to the HTML email which is created by CGI script 2, and thence sent to my AOL client side email account, I get: e.g.

      $subject is: {all blank}
      $comments is: {all blank}
      .......see, I'm expecting the same output results, as the inputs provided for subject and comments are the same (instead I get blanks)

      I got the idea to try this after my car was serviced and the service provider sent an email to my AOL email account that contains a satisfaction survey embedded in the body of the AOL email message. Since the service provider's survey worked, I'm inclined to believe the problem is with my code, not the security features on my email client. I hope this gives you a better feel for the problem. If anything still isn't clear, I'm happy to provide more information. Thank you for your patience.
        Well, I just tried this out. I sent the email from my work server, and picked it up at home, via Thunderbird.

        The URL pointed to your script, which I copied to a local machine here.

        I used the subject "Test" and the comment "Foo"

        when I clicked the submit button in the email I got:

        $subject is Test $content is

        The URL was correct: http://192.168.1.102/test/view_sent_form_data.cgi?subject=Test&content=Foo

        Further analysis shows that the form variable in your scripts is set to a different name than what you're trying to print. IE, you do this in your script that generates the email:

        Comments: <textarea name="content" rows="2" cols="15"></textarea>

        and this in your script that prints:

        my $comments = param("comments");

        So, I think you have a combination of things. 1) Your CGI parameter is wrong, and 2) AOL may not be letting you submit properly (or may be munging your HTML as was suggested above)

        Try sending it to a few different email clients. AOL is probably the most aggressive HTML mangler around, so it's quite likely you'll find that your form works elsewhere. If that turns out to be the case, take a close look at the form as it appears in AOL via "View Source". Also, you might take a close look at the form that did work - perhaps it does things in a way you can copy.

        -sam

Re: passing CGI form parameters via HTML email
by calin (Deacon) on Jun 21, 2006 at 20:20 UTC

    Some of your output goes to STDOUT, some goes to the sendmail pipe (MAIL filehandle).

    edit: Yeah, this is apparently not the problem. The programs prints to STDOUT as part of its execution as a CGI program, this output is not intended to go into the mail. Why it doesn't work? Two ideas:

    a) The MIME message is hand-crafted and possibly not well-formatted.

    b) The mail client dissalows form submission for security reasons.

      Thanks for your help. Any suggestion on an alternative way to better format the MIME message? Also, one other reply stated "Please stop shouting at us!" Was there something unintentional I did to warrant such an obvious disgruntled reply?
        Any text in ALL CAPITALS is considered shouting.
Re: passing CGI form parameters via HTML email
by MidLifeXis (Monsignor) on Jun 23, 2006 at 17:25 UTC

    I might also recommend checking out Mime::Lite. It may give you a little cleaner code when generating the email message.

    --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://556755]
Approved by calin
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-16 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found