Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

CGI form to email

by ezekiel (Pilgrim)
on Sep 13, 2004 at 04:07 UTC ( [id://390492]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I want to build a simple CGI form that will capture user input and email it to me. My initial thought (or lack of thought!) was, "this should be easy", hence:

MAIN: { my $cgi = new CGI; print $cgi->header(); my $html = qq| <form method="post" action="mailto:user\@host.com"> <input type="text" name="blah"> <input type="submit"> </form> |; print $html; }

Of course, that works to one extent, but is not what I had in mind i.e., I want the content of the form to be directly mailed to me - not launch a prepopulated, local mail client.

I assume I need to use Mail::Sendmail or something similar. Any tips as to where to start? Thanks.

Replies are listed 'Best First'.
Re: CGI form to email
by davido (Cardinal) on Sep 13, 2004 at 04:52 UTC

    The following script is one that I use occasionally when I need a self-mailer. It is self-contained: a single script outputs the HTML form, takes the input, and outputs the success/failure page. It takes the precaution of not accepting the recipient's email address via the form. Accepting recipient email addresses via an HTML form exposes you to malicious people creating their own clients that will interact with your script to turn it into a spam relay. This script won't let that happen (I'm fairly certain).

    I wrote it pretty early on in my Perl learning curve, so I apologize in advance for any inelegance.

    In particular, if I had it to do over again, I would have written this using MIME::Lite. I've gotten so that I prefer it over Mail::Mailer. But as they say in Portuguese, "Tanto faz" (either will do).

    I would also do away with the CGI::Carp stuff after the development phase was overwith.

    I also wrote this before I understood HTML::Template, so it reads header and footer files to allow for poor-man's site templates.

    Again, if I were rewriting it, I would do a lot more to separate the HTML from the code.

    But notwithstanding those caviets, look at it as a good starting point. It gets the job done for me when my needs are fairly simple... Here it is: selfmailer.cgi.

    For an example of this script in use, you may visit http://davido.perlmonk.org/cgi-bin/selfmailer.cgi


    Dave

      Dear davido,

      I am sorry to have to intrude you back again after this wonderful old post of yours.
      First of all can you enable the above script again from your server? Since when I tried it here, it gave error.

      I think because it fail to recognize this line:
      my ( $tof_file, $bof_file ) = ( '', '../site_templates/bof.shtml' );
      I have the similar problem with OP. The following script does a simple processing. It try to send the input values by email. Initially with "mailx" command under linux, then with Mail::Mailer as you suggested.

      But why my script below fail to send the email?
      The script below also accessible in the following site:
      __BEGIN__ #!/usr/bin/perl -wT use CGI ':standard'; use Data::Dumper; print header, start_html('Order Ice Cream with Price'), h1('Order Ice Cream with Price'); generate_form(); print_results() if param(); print end_html(); sub print_results { print b('Customer name: '), param('customer'), br; my $ct = param('cone'); my $nu = param('no_unit'); my $uemail = param('user_email'); my $subject = "Result"; if ( $ct && $nu ) { my $content = "You ordered $ct as many as $nu unit"; print $content,br; print "This message should be sent to your email address: ",u( +$uemail), " soon"; # Tried to send email here, but fail... # system("mailx -s $subject $uemail < $content"); # Tried this to send email also fail... # It says can't find module, but in my server # I have no problem doing: perl -c mailresult.cgi send_message( $uemail, "monkfan", "ewijaya\@i2r.a-star.edu.sg", $subject, $content ); } else { show_error_message(); } } sub send_message { # I deliberately remove $q here my ( $recipient_addr, $sender_name, $sender_email, $subject, $message ) = @_; my $mail = new Mail::Mailer; $mail->open( { To => $recipient_addr, From => $sender_email, Subject => "[Perlmonk Contact] " . $subject } ) or die "Can't open mail transport system: $!\n"; print $mail $message; close $mail or die "Can't close mail transport session: $!\n"; } sub show_error_message { print "Unit Too Large\n"; } sub generate_form { print hr, start_form, strong('Your name : '), textfield( -name => 'customer' ), br,br strong('Your email: '), textfield( -name => 'user_email'), br,br strong('Cone: '), radio_group( -name => 'cone', -multiple => 1, -values => [qw/sugar waffle/]),br,br strong('Number of Units: '), textfield( -name => 'no_unit'), br,br submit( -value => 'Send Order' ), end_form, hr; } __END__

      Regards,
      Edward
Re: CGI form to email
by ikegami (Patriarch) on Sep 13, 2004 at 04:14 UTC
    It's not easy. There are security considerations. Why don't you use the one from NMS, and modify it if need be.
Re: CGI form to email
by zdog (Priest) on Sep 13, 2004 at 04:23 UTC

    Take a look at MIME::Lite. It's simple and I've always found it useful.

    You'd use CGI to capture the input and then use this module (or some other: there are many options) to send the data on its way to your inbox.

    Zenon Zabinski | zdog | zdog@perlmonk.org

Re: CGI form to email
by csuhockey3 (Curate) on Sep 13, 2004 at 05:44 UTC
    Well, mine is certainly not as pretty or detailed as davido's, but I will share it anyway. Echo to the previous post, this is also pre-HTML::Template. However, it's easy to modify, it works, and I still use it every now and then when I need something quick and dirty.

    #!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw/ fatalsToBrowser /; # remove for production #Talk directly to sendmail, not using Mail::Sendmail $SENDMAIL_CMD = '|/usr/lib/sendmail -t -oi'; $cgi = new CGI; print $cgi->header; sub getData{ #get form data, $query->Vars(); } sub validate{ if (keys %errors){ while ( ($key, $value) = each %errors){ print $cgi->p("$key, <b>$value</b>\n"); } &displayErrorPage; }else{ &sendEmail; &displayConfirmationPage; } } sub sendEmail{ $emailBody = $emailBody . "EMAIL TEXT"; %mail = ( SMTP => 'localhost', from => 'from address', #userEmail to => "TO <$emailAddress>", cc => "CC <some cc address>", bcc => "Developer <developer@someAddress.com>", subject => "Subject Line", ); $boundary = "====" . time() . "===="; $mail{'content-type'} = "TEXT/plain; boundary=\"$boundary\""; $boundary = '--'.$boundary; $mail{body} = $mail{body} . $emailBody; sendmail(%mail); } sub displayErrorPage{ print $cgi->h1("Errors were present"); } sub displayConfirmationPage{ print $cgi->p("Thank You -- blah blah blah."); } sub sendmail{ my(%mail)= @_; open(MF, $SENDMAIL_CMD) || print "Sendmail Error! ($!)\n";; print MF<<__MAIL_EOF__; From: $mail{from} To: $mail{to} Bcc: $mail{bcc} Subject: $mail{subject} $mail{body} __MAIL_EOF__ }

  • Yet another way:

    sub sendEmail { my $to_field; my %param; foreach my $n ($query->param){ $param{$n} = $query->param($n); push @allStuff, $1; } &request(%param); } sub request{ my( %param )= @_; open(MF, $SENDMAIL_CMD); print MF<<__MAIL_EOF__; From: $param{from_email} To: $param{to_field}, $param{otherEmail} Subject: Subject Line Bcc: $Monitor_email #email message here.... __MAIL_EOF__

    as I said, there are more (and better) examples, but -- TIMTOWTDI :)

Re: CGI form to email
by reneeb (Chaplain) on Sep 13, 2004 at 07:44 UTC

Log In?
Username:
Password:

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

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

    No recent polls found