Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strip carriage returns & linefeed characters from form field

Status
Not open for further replies.

amanxman

Technical User
Oct 8, 2005
19
NZ
Hi everyone,

I have a little problem with a form to email which is getting spammed, and the solution, I've been told is to strip carriage returns (\r) and linefeed characters (\n) from form fields in my cgi scripts - with the following code:

$field =~ s/\r/ /g;
$field =~ s/\n/ /g;

for each field used in an email.


So, in my code, sample below, what do i change and where!! I tried;

print MAIL "To: $email =~s/\r /g"; but that didnt work...
maybe print MAIL "From: $domain\n\r"; ???

I don't know!!
what should it be?? If someone could amend the code below to include $field =~ s/\r/ /g; & $field =~ s/\n/ /g; for each field used in an email I would be INCREDIBLY grateful!!!

Thanks in advance
amx

CODE below:

sub mailMe
{
my($form,$data) = shift;
my($sendmail,$name,$domain,$email) =
('/usr/lib/sendmail','Mann Scuba Divers',' myemail@hotmail.com');
open MAIL, "|$sendmail $email" or die "Can't open sendmail: message = '$!'";
print MAIL "To: $email\n";
print MAIL "From: $domain\n";
print MAIL "Subject: $form->{subject}\n\n";
print MAIL "Dear $name,\n\n";
for(@{$form->{Seq}}) { print MAIL "$_\t\t : ",$form->{$_},"\n" and $data = 1 }
print MAIL "The input form was empty\n" unless $data;
print MAIL "\n\nYours truly,\nThe Mail Room";
close MAIL or die "Can't close sendmail: message = '$!'"
}
 
soli, I not really understand ur problem.

May be, you should do it this way?
Code:
...... .....
my($sendmail,$name,$domain,$email) =
     ( '/usr/lib/sendmail',
       'Mann Scuba Divers',
       '[URL unfurl="true"]www.mydomain.co.uk',[/URL]
       'myemail@gmail.com, myemail@hotmail.com');
[red]
$domain =~ s/[\r\n]/ /g;
$email =~ s/[\r\n]/ /g;
[/red]
open MAIL, "|$sendmail $email" or die "Can't open sendmail: message = '$!'";
print MAIL "To: $email\n";
..... ...... .....
 
Ah ha, I thought I needed to put the $xxxx =~ s/\r\n/ /g; at each instance of the fields (i.e. print MAIL "To: $email =~s/\r /g"; for example)

Do i need the square brakets [] aswell?

Cheers for your help
Can't test it yet, but will try it later

amx
 
A little more explanation:
Code:
$AA=~s/\n\r/ /g;

$BB=~s/[\n\r]/ /g;

$CC=~tr/\n\r/ /;

$DD=~s/\n/ /g;$DD=~s/\r/ /g;

$EE=~s/[\n\r]+/ /g;

OK, for $AA, only those have exactly "\n" followed by "\r" are replaced. those with only one "\n" , one "\r" or "\r\n" aren't replaced. (BAD)

for $BB, all single "\n", "\r" will be replaced with one space. While "\n\r" and "\r\n" replaced with 2 spaces. (NOT VERY GOOD, may be?)

for $CC adn $DD, same as $BB.(NOT VERY GOOD, may be?)

for $EE, all "\n" and "\r" will be replaced with one space. no matter how "\r\n", "\n\r", "\r" or "\n". (BETTER?).

;-)
 
think i understand that!

will play with the script later

cheers for your help :)

amx
 
Incidentally, this is why piping directly to sendmail is a recipe for disaster and is not recommended. Either use one of the many proper mailing modules from CPAN or fall back to NMS Formmail, which is available here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top