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

I can't figure this out. No errors appear and it redirects back to the page it's supposed to. It's supposed to email the admin and send a thank you email to the one who filled out the contact form.

Neither emails are sent or recieved but it seems to be fine (it loads, processes and redirects without a problem). Any idea what might be causing this?

#!/usr/bin/perl use warnings; use strict; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); ####################################### # Configuration section # ####################################### my $admin_mail = 'my@email.com'; # change the above to the email address of the administrator my $sendmail = '/usr/lib/sendmail'; # change the above to the location of sendmail on the server my $thank_you_msg = 'Dear Client,\nThank you for contacting Ufly4less. + We aim to respond to all enquires within 1 working day.\n\nRegards,\ +nThe Ufly4less Team\n\n100% committed to finding you cheap fares, everytime!'; # change the above to the thank you message you want to send # your visitors. Add a \n anywhere you want in the message # to create new lines. \n\n would make a double space. my $redirect_url = 'http://www.ufly4less.com/default'; # change the above to the page you want to redirect to ####################################### # Do not edit below this line # ####################################### ######## # This section only initates when the form was submitted ######## if (param()) { print header; my $name = param("name"); my $organisation = param("organisation"); my $address = param("address"); my $town = param("town"); my $county = param("county"); my $postcode = param("postcode"); my $phone = param("phone"); my $email = param("email"); my $message = param("other"); ###### # make sure our required fields are filled out ###### if ($name eq "") { &reqerror("name"); } if ($phone eq "") { &reqerror("phone"); } if ($postcode eq "") { &reqerror("postcode"); } if ($email eq "") { &reqerror("email");} ###### # Email function starts here ###### open(MAIL,"| $sendmail") or die "Can't open $sendmail sendmail beca +use: $!"; print MAIL "To: $admin_mail\n"; print MAIL "From: $email\n"; print MAIL "Subject:Website Enquiry\n"; print MAIL "Contact Name: name\n"; print MAIL "Enquiry Type: $organisation\n"; print MAIL "Address: $address\n"; print MAIL "Town: $town\n"; print MAIL "Postcode: $postcode\n"; print MAIL "County: $county\n"; print MAIL "Phone: $phone\n"; print MAIL "Email: $email\n\n"; print MAIL "Other Details: $message\n"; print MAIL "\n\n"; close(MAIL); open(MAIL,"| $sendmail") or die "Can't open $sendmail sendmail beca +use: $!"; print MAIL "To: $email\n"; print MAIL "From: $admin_mail\n"; print MAIL "Subject:Ufly4Less Website Enquiry\n"; print MAIL "$thank_you_msg \n\n\n"; print MAIL "\n\n"; close(MAIL); ###### # Printout of confirmation page ###### print <<" END"; <HTML> <head> <title></title> <meta http-equiv="refresh" content="1;url=$redirect_url"> </head> <body bgcolor="white" text="black" link="blue" vlink="purple" ali +nk="red"> <p>Thank you for your message. You are now being redirected.</ +p> </body> </HTML> END exit; } ####### # If this section is activated, no form data was submitted ####### else { print header, start_html(); print qq(<font color="red"><b>ERROR:</b></font> This page cannot be + accessed directly. Action cancelled.); exit; } #################### # Error sub routine found below #################### sub reqerror { print header, start_html(); my $error = shift; print qq(<font color="red"><b>ERROR!</b> The field $error was left +blank and is a required field. Please click back to try again.); exit; }

Edit g0n - added readmore tags

Replies are listed 'Best First'.
Re: contact form not mailing (urgent!)
by Corion (Patriarch) on Jul 07, 2005 at 21:21 UTC

    I know that this is likely outside of your scope, but you should retire this CGI script as soon as possible. It contains a flaw that makes it trivially easy to send spam via your mail address to any address, because you do not check that your input parameters are well-formed. Especially, no parameter should be allowed to contain a newline (\n) character.

    As for your error tracking, try looking into the sendmail logfiles wherever they are, and make sure that sending mail works from the command line before trying to use it from within the webserver. You can also print out the mail into a text file and later manually pipe the text file into sendmail to see the error messages.

    You can of course also look at what the NMS Formmail Script has to offer.

      Sendmail works just fine on my server. There are a number of other forms that are working. I do know this isn't secure yet but once the error is fixed, I'll begin tweaking the rest of it.

      Forgot to mention this.. This form is being sent data via POST from another web site completely. Does this matter at all? I doubt it makes any difference, but just thought I'd ask.

        I know this is a tough situation for you. Please take five minutes to step back from the problem at hand and relax.

        intermission

        Welcome back.

        Did you follow the steps that I outlined? Especially the step of redirecting the output into a file and then piping the file to sendmail, to see the error message(s)? My guess is, that you are not delimiting the email headers from the email message with an empty line as is customary. But that is just a guess since I am a long time user of MIME::Lite which takes care of these pesky details for me. So please, do follow the outlined steps for debugging, as it will make the job easier, now and in the future, for you and me.

Re: contact form not mailing (urgent!)
by BaldPenguin (Friar) on Jul 07, 2005 at 21:31 UTC
    Is this script working anywhe else?
    As Corion said above, check your maillog. The errors sounds like sendmail may have relaying locked down, at least that's what has happened to me in the past, Maybe the apache user is prevented from mailing as a security measure. Perhaps you need to use something that will allow you to pass user and password information to let you mail out. What OS and server config?

    Don
    WHITEPAGES.COM | INC

    Edit by castaway: Closed small tag in signature

Re: contact form not mailing (urgent!)
by gellyfish (Monsignor) on Jul 08, 2005 at 09:43 UTC

    The actual reason that the mail isn't being sent is that you are not telling the sendmail what to do, you have neither told it the recipient on the command line nor specified the -t switch to get it to obtain the recipient from the headers of the message you are piping in.

    If you set your $sendmail variable to:

    /usr/lib/sendmail -oi -t
    Then it should work.

    That all said I think you are almost certainly better off using one of the NMS programs to do this, not least because they are probably more secure than you will ever make your own program and also because they are well supported.

    But I would say that.

    /J\

Re: contact form not mailing (urgent!)
by stonecolddevin (Parson) on Jul 08, 2005 at 04:08 UTC
    I would recommend looking at MIME::Lite or search CPAN for another mailer module...

    MIME::Lite is the easiest module I've used as far as mailing goes, it's simple and has some easy to use methods.
    meh.