PHP联系表单不发送电子邮件

时间:2015-03-27 11:57:32

标签: php forms contact

我一直在尝试为我的网站提供联系表单,而且似乎是。据我所知,没有任何错误,并且在发送表单时会显示成功消息,但我的收件箱中没有电子邮件。我在PHP方面不是很强,所以有谁能告诉我我做错了什么?

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

$email_to = "myemail@mydomain.dk";

$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);  

?>

这是我的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>

2 个答案:

答案 0 :(得分:1)

检查您的编码是这样的。这是通过php发送电子邮件的基本方法。

示例代码如下:     

  //Email information
  $admin_email = "someone@example.com";
  $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
  }
?>

答案 1 :(得分:0)

在我看来,如果你纠正了一个小错误你的实现是有效的(但我坚持你应该更好地使用Abdulla的例子)。

缺少&#34;}&#34;对于你的第一个,它永远不会关闭。如果你在PHP(@)中使用supressor Tag,你永远不会知道你的邮件是否给你一个错误。如果您想了解有关邮件错误的更多信息:How can I catch an error caused by mail()?

如果它仍然不起作用,我会考虑检查你的phpinfo();如果邮件设置正确。