Amalie
Amalie

Reputation: 292

PHP contact form not sending e-mail

I have been trying to get a contact form working for my site, and it seems to be. There are no errors as far as I can see, and the success-message is shown when the form is sent, but no e-mail lands in my inbox. I am not very strong in PHP, so can anyone tell me what I am doing wrong?

<?php
if(isset($_POST['email'])) {

$email_to = "[email protected]";

$email_subject = "Itesso ELS kontaktform";





function died($error) {

    // your error code can go here

    echo "Beklager, men der er et problem med de felter du har udfyldt.";

    echo "Du kan se fejlene markeret nedenfor.<br /><br />";

    echo $error."<br /><br />";

    echo "Venligst ret og prøv igen.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['telephone']) ||

    !isset($_POST['comments'])) {

    died('Beklager, men der er et problem med de felter du har udfyldt.');       

}



$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['telephone']; // not required

$comments = $_POST['comments']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'Du har indtastet en ugyldig e-mail.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {

$error_message .= 'Du har indtastet et ugyldigt fornavn.<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'Du har indtastet et ugyldigt efternavn.<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'Du har indtastet en ugyldig kommentar.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Detaljer nedenfor.\n\n";



  function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

  }



$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Telephone: ".clean_string($telephone)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>

And here is my html:

<form name="contactform" method="post" action="send_form_email.php">

<table width="450px">

<tr>

<td valign="top">

<label for="first_name">First Name *</label>

</td>

<td valign="top">

<input  type="text" name="first_name" maxlength="50" size="30">

</td>

</tr>

<tr>

<td valign="top"">

<label for="last_name">Last Name *</label>

</td>

<td valign="top">

<input  type="text" name="last_name" maxlength="50" size="30">

</td>

</tr>

<tr>

<td valign="top">

<label for="email">Email Address *</label>

</td>

<td valign="top">

<input  type="text" name="email" maxlength="80" size="30">

</td>

</tr>

<tr>

<td valign="top">

<label for="telephone">Telephone Number</label>

</td>

<td valign="top">

<input  type="text" name="telephone" maxlength="30" size="30">

</td>

</tr>

<tr>

<td valign="top">

<label for="comments">Comments *</label>

</td>

<td valign="top">

<textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

</td>

</tr>

<tr>

<td colspan="2" style="text-align:center">

<input type="submit" value="Submit">

</td>

</tr>

</table>

</form>

Upvotes: 0

Views: 146

Answers (2)

David Pinezich
David Pinezich

Reputation: 134

In my opinion your implementation works if you correct a small error (but I insist you should better use Abdulla's example).

There is a missing "}" for your first if, it is never closed. And if you use the supressor Tag in PHP (@) you will never know if your mail has given you an error. If you want to read more about mail errors: How can I catch an error caused by mail()?

If it still does not work, I would consider to check your phpinfo(); if mail is set up correctly.

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38609

Check your coding something like this. This is basic method of sending E-Mail through php.

Example code is below:

  //Email information
  $admin_email = "[email protected]";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];

  //send email
  mail($admin_email, "$subject", $comment, "From:" . $email);

  //Email response
  echo "Thank you for contacting us!";
  }

  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>

<?php
  }
?>

Upvotes: 1

Related Questions