Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

acc

[Resolvido] Formulário de Contato

Recommended Posts

Estou mexendo em um tema do Wordpress que veio um formulário Ajax que está funcionando tudo, menos a validação de e-mail, segue os códigos abaixo:

 

contact.php

<?php
/*
Template Name: Contact
*/
?>

<?php get_header(); ?>
<script type="text/javascript">
	 $(document).ready(function(){
		  $('#contact').ajaxForm(function(data) {
			 if (data==1){
				 $('#success').fadeIn("slow");
				 $('#bademail').fadeOut("slow");
				 $('#badserver').fadeOut("slow");
				 $('#contact').resetForm();
				 }
			 else if (data==2){
					 $('#badserver').fadeIn("slow");
				  }
			 else if (data==3)
				{
				 $('#bademail').fadeIn("slow");
				}
				});
			 });
	</script>
<!-- begin colLeft -->
<div id="colLeft">

		<h1>Contact Us</h1>
		<p><?php echo stripslashes(stripslashes(get_option('journal_contact_text')))?></p>

		<p id="success" class="successmsg" style="display:none;">Your email has been sent! Thank you!</p>

		<p id="bademail" class="errormsg" style="display:none;">Please enter your name, a message and a valid email address.</p>
		<p id="badserver" class="errormsg" style="display:none;">Your email failed. Try again later.</p>

		<form id="contact" action="<?php bloginfo('template_url'); ?>/sendmail.php" method="post">
		<label for="name">Your name: *</label>
			<input type="text" id="nameinput" name="name" value=""/>
		<label for="email">Your email: *</label>

			<input type="text" id="emailinput" name="email" value=""/>
		<label for="comment">Your message: *</label>
			<textarea cols="20" rows="7" id="commentinput" name="comment"></textarea><br />
		<input type="submit" id="submitinput" name="submit" class="submit" value="SEND MESSAGE"/>
		<input type="hidden" id="receiver" name="receiver" value="<?php echo strhex(get_option('journal_contact_email'))?>"/>
		</form>

</div>
<!-- end colleft -->

		<?php get_sidebar(); ?>	

<?php get_footer(); ?>

 

sendmail.php

<?php
 	$str = $_POST["email"];
 		if(isset($_POST['submit'])) {
         error_reporting(E_NOTICE);
         function valid_email($str)
         {
         return ( ! preg_match("/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$/", $str)) ? FALSE : TRUE;
	     }
         if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
         {
             //$to = preg_replace("([\r\n])", "", $_POST['receiver']);
		  $to = preg_replace("([\r\n])", "", hexstr($_POST['receiver']));
		  $from = preg_replace("([\r\n])", "", $_POST['email']);
		  $subject = "Website contact message from ".$_POST['name'];
             $message = $_POST['comment'];

		  $match = "/(bcc:|cc:|content\-type:)/i";
			if (preg_match($match, $to) ||
				preg_match($match, $from) ||
				preg_match($match, $message)) {
			  die("Header injection detected.");
			}
             $headers = "From: ".$from."\r\n";
  			  $headers .= "Reply-to: ".$from."\r\n";

       if(mail($to, $subject, $message, $headers))
             {
                 echo 1; //SUCCESS
             }
             else {
                 echo 2; //FAILURE - server failure
             }
         }
         else {
      	  echo 3; //FAILURE - not valid email

         }
	  }else{
		 die("Direct access not allowed!");
	   }

	    function hexstr($hexstr) {
			  $hexstr = str_replace(' ', '', $hexstr);
			  $hexstr = str_replace('\x', '', $hexstr);
			  $retstr = pack('H*', $hexstr);
			  return $retstr;
			}

     ?>

 

Já tentei mudar a forma de validação e não consegui consertar...

Compartilhar este post


Link para o post
Compartilhar em outros sites

tenta substituir

  return ( ! preg_match("/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$/", $str)) ? FALSE : TRUE;

 

if(filter_var($str, FILTER_VALIDATE_EMAIL)){
   return true;
}else{
   return false
} 

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.