nms formmail - no email validation if not required + separate redirect page

400 Views Asked by At

I have setup nms formmail recently on my blog and I made email optional, however I would like to make sure that when it is filled out, an email validation check is run. It looks like nms formmail only offers this check in case the email field is required.

I would like to modify the cgi script using it's own validate_email function and redirect to a "email not valid" page in case the email field is not empty and the field is not valid.

Note that I have added the following code already that takes care of adding a standard email in case the field is empty, so the check should probably come after this line somewhere: $email = '[email protected]' unless $email;

inside this bit of code:

sub send_main_email {
  my ($self, $date, $email, $realname) = @_;

  my $mailer = $self->mailer;
  $mailer->newmail($self->name_and_version, $self->{CFG}{postmaster}, @{ $self->{Recipients} });

  $email = '[email protected]' unless $email;

  $self->send_main_email_header($email, $realname);
  $mailer->print("\n");

  $self->send_main_email_body_header($date);

  $self->send_main_email_print_config;

  $self->send_main_email_fields;

  $self->send_main_email_footer;

  $mailer->endmail;
}

I tried doing the following under [email protected]' unless $email;:

if (validate_email($email) = 0) {
    print $self->cgi_object->redirect($self->{FormConfig}{'bad_email'});
}

...but it returned this error: Can't modify non-lvalue subroutine call at (eval 8) line 1062.

this is the HTML I'm using:

<form method=post action="http://mydomain.com/formmail.cgi">
<label for="realname">name (optional)</label><br />
<input type=text name="realname" /><br />
<label for="email">e-mail (optional)</label><br />
<input type=text name="email"/><br />
<label for="text">text (required)</label><br />
<textarea name=text></textarea><br />
<input type=submit value="send">

<input type=hidden name="redirect" value="http://mydomain.com/thank-you.html">
<input type=hidden name="subject" value="Email form submitted">
<input type=hidden name="required" value="text">
<input type=hidden name="missing_fields_redirect" value="http://mydomain.com/missing-field.html">
<input type=hidden name="bad_email" value="http://www.google.com/">
<input type=hidden name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">
<input type=hidden name="sort" value="order:contact,text">
</form>

Note that I have no specific knowledge in perl, but I'm pretty fast in picking up new code language, so please bear with me, and try to be as detailed as possible (preferably with example code instead of describing what you think, unless it makes more sense of course).

1

There are 1 best solutions below

5
On

if (validate_email($email) = 0) should be if ( not validate_email($email) ) { or if ( !validate_email($email) ) { or if ( 0 == validate_email($email) ) { because = is assignment operator and == is numeric-equality operator

that covers the syntax error, all I have time for :) also "valid_email" sounds better read out loud ( if not valid_email [email protected] then redirect to badmailpage