Welcome aboard patb, good job for a beginner! The short answer: wrap the send line in an else and last out of the loop IF spam is found.
A longer answer: there's more to form spam than just http. Added lines below, it may need debugging but as you see it allows you to filter for a number of patterns. Also the "bad" part about this approach is you won't know what they are sending - I usually choose to
log the input and exit the program from the logging routine. But anyway . . . .
sub send_main_email_fields {
my ($self) = @_;
my $spam = 0;
my @bad_patterns (
'b*cc\s*:', ## multipart or mail header injection attempts
'to\s*:',
'content\-type',
'\[\s*URL.*\]*', ## BB code style attempts
'\[\s*LINK.*\]*',
'\%5B\s*URL.*(\%5D)*',
'\%5B\s*LINK.*(\%5D)*',
'\[\s*a\s*href.*\]*',
'\%5B\s*a\s*href.*(\%5B)*',
'\<\s*a\s*href.*\>*',
'\%3C\s*a\s*href.*(\%3E)*',
#'example.com',
'viagra',
'pharm',
'male\s+enhance'
);
#
foreach my $f (@{ $self->{Field_Order} }) {
my $val = (defined $self->{Form}{$f} ? $self->{Form}{$f} : '');
foreach my $pattern (@bad_patterns) {
if ($val =~ /$pattern/i) {
$val = 'SUSPECTED SPAM - MESSAGE DELETED';
$spam=1;
last;
} # end if
} # end @bad_patterns
if ($spam==1) {
print "content-type: text/html\n\n";
print '<p>Program error. Please contact us by email or
phone. <br><br>Press your browser's
"Back" button to continue.</p>';
exit 0;
last;
} # end if $spam == 1
$self->send_main_email_field($f, $val);
} # end Field_Order
} # end sub
As you can see, this allows you to add and remove patterns as needed and picks up a couple others. You can add http(s)? if you need to. The "example.com" one you'd use for your domain (you'll never send email to yourself except in testing, which is why it's commented out) and is a common attack, "anything@yourdomain.com".
The $spam=1 is because we want to
last out of the outer foreach and it would only last out of the inner loop if we just "last'ed" from there.