Hi everyone Could you help me on a php problem please.

I am trying to get a php email contact script to work, I have inserted my email in below (example@example.com) but when i fill the form in i get a error that the email is invalid.

First I have the form that post to the script (Below)

 <form action="contact.php" method="post" id="contactform">
          <ol>
            <li>
              <label for="name">Your Name</label>
              <input id="name" name="name" class="text" />
            </li>
            <li>
              <label for="email">Your email</label>
              <input id="email" name="email" class="text" />
            </li>
            <li>
              <label for="company">Company</label>
              <input id="company" name="company" class="text" />
            </li>
            <li>
              <label for="subject">Subject</label>
              <input id="subject" name="subject" class="text" />
            </li>
            <li>
              <label for="message">Commet</label>
              <textarea id="message" name="message" rows="6" cols="50"></textarea>
            </li>
            <li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
              <div class="clr"></div>
            </li>
          </ol>
        </form>

here is the script for the contact.php

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "example@example.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!'; 
} else {
echo 'ERROR!';
}
}
?>
    if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
    }

    That's a pretty complicated pattern. Are you sure it's correct? Also, eregi has been deprecated. You should probably use preg_match instead.

      sneakyimp;10951720 wrote:

      That's a pretty complicated pattern.

      It's actually nowhere close to a pattern that matches RFC standards. For example, brad+grafelman@example.com is a perfectly valid e-mail address according to RFC standards, but the above pattern as well as many, many websites reject it as invalid.

      EDIT: For example, the following is apparently very close to matching valid e-mail addresses according to RFC 2822:

      [a-z0-9!#$%&'*+/=?\^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?\^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

        Yes, I remember weedpacket sending me some torturous RFCs about urls in this thread.

        Here is brad's regex in the context of your code:

        if (!preg_match('/[a-z0-9!#$&#37;&\'*+\/=?\^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?\^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/', $email)) {
          $error.="Invalid email address entered";
          $errors=1;
        } else {
          // email is ok!
        }
        

        Maybe try that?

          Hey, it's not my regexp (although I did fail to cite my source)... I only brought the topic up because I actually use an e-mail address like the one I posted above for my GMail account.

          For example, if I were registering for a website and I was suspicious as to whether they would share my address (or send their own spam) with others, I might enter my e-mail address as myaccount+PHPBuilder@gmail.com, which GMail will automatically forward to myaccount@gmail.com. If I start getting V14GR4 SPECIAL OFFER! e-mails sent to myaccount+PHPBuilder@gmail.com... not only do I know the source, but I can also block all incoming mail addressed to myaccount+PHPBuilder@gmail.com.

            Actually now that I look at that regex again, I see that it's missing the ^ at the beginning and a $ at the end -- meaning that if it does in fact match a valid email address, it will return TRUE if a string merely contains a valid email address.

              sneakyimp;10951741 wrote:

              Actually now that I look at that regex again, I see that it's missing the ^ at the beginning and a $ at the end -- meaning that if it does in fact match a valid email address, it will return TRUE if a string merely contains a valid email address.

              I think it was left open-ended on purpose (notice that the original didn't even include delimiters either). For example, you're right - the beginning- and end-of-string markers should be used to ensure that the string passed contains a valid e-mail address and only a valid e-mail address.

              But that's specific to this application of the pattern; others might want to add \b word boundary placeholders and use preg_match_all() to instead extract all valid addresses out of a string.

              EDIT: You should also probably add the 'i' modifier, so that cuppercasel letters are allowed as well.

                let php do the heavy work for you and use filter_var()

                var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));

                  ok I added the code below

                  <?php
                  
                  if(!$_POST) exit;
                  
                  $email = $_POST['mail'];
                  
                  if (!preg_match('/[a-z0-9!#$%&\'*+\/=?\^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?\^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/', $email)) {
                    $error.="Invalid email address entered";
                    $errors=1;
                  } else {
                    // email is ok!
                  } 
                  
                  if($errors==1) echo $error;
                  else{
                  	$values = array ('name','email','message');
                  	$required = array('name','email','message');
                  
                  $your_email = "mail@steve-berrill.com";
                  $email_subject = "New Message: ".$_POST['subject'];
                  $email_content = "new message:\n";
                  
                  foreach($values as $key => $value){
                    if(in_array($value,$required)){
                  	if ($key != 'subject' && $key != 'company') {
                  	  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
                  	}
                  	$email_content .= $value.': '.$_POST[$value]."\n";
                    }
                  }
                  
                  if(@mail($your_email,$email_subject,$email_content)) {
                  	echo 'Message sent!'; 
                  } else {
                  	echo 'ERROR!';
                  }
                  }
                  ?>

                  THEN i GOT THIS ERROR ON THE PAGE

                  Invalid email address entered

                    this line is different than your original code:

                    $email = $_POST['mail']; 

                    The input for the email address on your form has a name attribute of 'email', so that line should be:

                    $email = $_POST['email']; 

                    Are you certain that the email address is being defined properly in the var $_POST['email'] ?

                    PS: Dagon, WHO KNEW ABOUT FILTER_VAR?? amazing.

                      thanks allot, it works like a charm. I really appriciate all your help

                        Don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu above.

                          Write a Reply...