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

Hi, I have a script that accepts post data from a form on a website for car part requests. i am having a problem with the e-mail field. sometimes it logs the email address correctly but sometimes it replaces the @ sign with %40. here is the script,

sub email{ $smsreply = 'd:\inetpub\wwwroot\cgi-bin\classads\test'; { local ($buffer, @pairs, $pair, $name, $value, %FORM)}; # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CON +TENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value = +~ tr/+/ /; $postFields{ "$name" } = $value; #populatePostFields; $from = $postFields{ "name" }; $msg = $postFields{ "DESC" }; $email = $postFields{ "email" }; $telno = $postFields{ "tel" }; $telnob = $postFields{ "telb" }; $telno = $postFields{ "tel" }; $cc = $postFields{ "CC" }; $vyear = $postFields{ "VYEAR" }; $gears = $postFields{ "gears" }; $make = $postFields{ "make" }; $model = $postFields{ "model" }; $doorno = $postFields{ "doors" }; $fuel = $postFields{ "fuel" }; if ($gears =~ auto){$gears = Automatic} else {$gears = "$gears Speed M +anual"}; $msg =~ s/%../ /g; } open EMAILREQNO, "$emailreqno" ; ($emailref)= <EMAILREQNO>; chomp($emailref); close EMAILREQNO ; $nextemailno = ($emailref+1); $extn = ".dat"; $emailfile = "E$emailref$extn"; $emailtime = "$dayname $monnm $Day $Hour$sep2$Minute$sep2$Second $year +x"; open (EMAIL, ">$emailreqpath/$emailfile"); print EMAIL "EMAIL REQUEST \n"; print EMAIL "$from \n"; print EMAIL "\n"; print EMAIL "$email \n"; print EMAIL "$telno \n"; print EMAIL "$telno \n"; print EMAIL "$vyear $make $model $doorno Door $cc CC $fuel $gears $msg + \n"; print EMAIL "\n"; print EMAIL "\n"; print EMAIL "\n"; print EMAIL "\n"; print EMAIL "\n"; print EMAIL "\n"; print EMAIL "WWW \n"; print EMAIL "$emailtime\n"; print EMAIL "EMAIL Request \n"; print EMAIL "Email Requests \n"; print EMAIL "WWW \n"; print EMAIL "\n"; print EMAIL "EMAIL \n"; close EMAIL; &oops('cannot change email ref number') unless (open(EMAILB, ">$emailr +eqno")); print EMAILB "$nextemailno"; close EMAILB; open REQORDER, "$reqordernum" ; ($reqorder)= <REQORDER>; chomp($reqorder); close REQORDER ; $nextreqorderno = ($reqorder+1); &oops('cannot create Request book entry') unless (open(NEWITEMA, ">$re +qorderpath/$reqorder")); print NEWITEMA "E$emailref\n"; close NEWITEMA; &oops('cannot change req number order') unless (open(REQNUMORDER, ">$r +eqordernum")); print REQNUMORDER "$nextreqorderno"; close REQORDERNUM; print "Content-type: text/html\n\n"; print <<"EOF"; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style1 { font-size: 36px; color: #FF0000; } .style2 {font-size: 24px} .style3 {font-size: 36px} .style4 {color: #FF0000} --> </style> </head> <body> <table width=700><TR><TD> <div align="center"> <p class="style1">Thank you for your enquiry.</p> <p class="style2">This has been logged on our system. we will get ba +ck to you shortly.</p> <p class="style2">Your Reference number is <span class="style4">E$em +ailref</span> please make a note of this.</p> <p class="style2">You will need to quote this should you need to con +tact us about your enquiry.</p> <p class="style2">Our telephone number for all enquiries is</p> <p class="style1"> 08454084000</p> </div> </TD></TR></table> </body> </html> EOF }

Everything else works perfectly just sometimes i get this e-mail problem. Any Help would be appreciated. Thanks Michael

Replies are listed 'Best First'.
Re: Email Address in Post Replies,
by JavaFan (Canon) on Dec 16, 2008 at 12:00 UTC
    Well, the user agent is free to replace a '@' with %40. It's your code that has to cope with it. And since you're doing all the work yourself, you'll have to fix it yourself.

    Why on earth are you handrolling all this parsing yourself? You aren't doing it correctly (otherwise, you wouldn't ask the question you're asking), and there are modules that do it better. One of them even comes with perl. It's called "CGI".

Re: Email Address in Post Replies,
by n3toy (Hermit) on Dec 16, 2008 at 15:41 UTC
Re: Email Address in Post Replies,
by Bloodnok (Vicar) on Dec 16, 2008 at 11:59 UTC
    It would help if you could elaborate on the circumstances under which the script does & doesn't work e.g. sample working/failing input etc.

    A user level that continues to overstate my experience :-))
Re: Email Address in Post Replies,
by Sagacity (Monk) on Dec 16, 2008 at 15:49 UTC
    Hi dmsparts,

    This looks like a "Inherited Script" scenario where you didn't create this script back in the late 90's when a functional approach to scripting in Perl was the usual method. Been there, Done that!

    So, let's answer the question. The hexidecimal (ASCII) value is what you're seeing, @ = %40. So, simply use a substitution statement to correct the problem.

    $email = $postFields{ "email" }; $email =~ s/(\%40)/\@/gi;
    I added grouping as well as a global search and case insignificant modifiers.

    Hope this helps, Good Luck

      I'm not sure this is good advice. Yes, this does seem to solve the current problem, but it makes the code a little more fragile and even less likely to be correct in the future.

        Chromatic,

        I would agree that this code in it's entirety needs to be brought up to speed as far as using hashes, packages and a more current style of scripting.

        I don't see where using substitution adds instability or has a problem with accuracy, unless they change the ASCII value for the character '@'.

        If I were the OP, and had a choice of re-writing this script, or fixing it for now and coming back to it later, I would code the fix and enjoy the Holiday's.
        Sometimes, getting all fancy for such a little straight forward script is not worth the effort.