Email Form not Working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrCapuchino
Forum Newbie
Posts: 2
Joined: Wed Dec 08, 2010 3:43 pm

Email Form not Working

Post by MrCapuchino »

Hello,

I'm new to PHP, still I'm trying a code that I got from a website, I tested in a page I own and it redirects to the sucess web page, the thing is that I never receive the email, not even in the junk folder or anything. I tried different email adressess including one with my domain extension.

In this example I put a fake email address but I'm using an address that it exists.

What can be wrong if it seems that is working.

Here is the code

Code: Select all

<?php
if(!isset($_POST['submit']))
{
	//This page should not be accessed directly. Need to submit the form.
	echo "error; you need to submit the form!";
}
$name = $_POST['nombre'];
$visitor_email = $_POST['correo'];
$message = $_POST['comentarios'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = '$visitor_email';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".
    
$to = "myemailadress@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: http://address.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
   
?> 
thanks in advance.
Last edited by MrCapuchino on Wed Dec 08, 2010 4:06 pm, edited 1 time in total.
MrCapuchino
Forum Newbie
Posts: 2
Joined: Wed Dec 08, 2010 3:43 pm

Re: Email Form not Working

Post by MrCapuchino »

Here is the html code for the form.

<form action="contacto.php" method="post" name="forma_contacto" id="forma_contacto">
<p>
<label for="nombre">Nombre:</label>
<input name="nombre" type="text" id="nombre" />
</p>
<p>
<label for="correo">Correo:</label>
<input type="text" name="correo" id="correo" />
</p>
<p>
<label for="comentarios"></label>
<textarea name="comentarios" id="comentarios" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Enviar" />
<input type="reset" name="reset" id="reset" value="Borrar" />
</p>
</form>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Email Form not Working

Post by social_experiment »

Are you getting any error messages? A side note on your security with regards to checking if the submit button has been clicked: If you check that it isn't clicked

Code: Select all

<?php
if(!isset($_POST['submit']))
{
        //This page should not be accessed directly. Need to submit the form.
        echo "error; you need to submit the form!";
}
?>
it is prudent to have an else option.

Code: Select all

<?php
if(!isset($_POST['submit']))
{
        //This page should not be accessed directly. Need to submit the form.
        echo "error; you need to submit the form!";
}
else {
        // send mail, check variables, blah blah
}
?>
In this way, you give your script an option. I tested the script (without the html form) and i simply altered the values of the variables and it sent the email. Just a little point to take into consideration :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply