http://qs321.pair.com?node_id=390500


in reply to CGI form to email

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 :)