我的联系表格不发送邮件

时间:2013-02-05 11:12:44

标签: php html contact-form

我已将这两个文件上传到服务器,但是当我提交表单时,感谢邮件会出现,但收件箱中没有邮件。请帮助我。

我的Html页面是

<form name="htmlform" method="post" action="html_form_send.php">
<table width="450px">

</tr>





<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">
</td>
</tr>
</table>
</form>

我已将这两个文件上传到服务器,但是当我提交表单时,感谢邮件会出现,但收件箱中没有邮件。请帮助我。

我的Php编码是

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

    // CHANGE THE TWO LINES BELOW
    $email_to = "anshu_mah@yahoo.co.in";

    $email_subject = "website form submissions";


    function died($error) {
        // your error code can go here
        echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<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('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $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 .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\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);  
?>

<!-- place your own success html below -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
die();
?>

2 个答案:

答案 0 :(得分:0)

邮件可能会被传入服务器阻止,因为它看起来像发件人无效,看起来类似于垃圾邮件。

如果您有权访问它,请检查服务器上的过滤器日志。这应该在cPanel中作为“跟踪电子邮件”提供。如果您的邮件被阻止,您会在列表中看到它并带有标记,表示存在问题,将鼠标悬停在该邮件上,您将看到原因。

如果是这种情况,并且原因类似于“发件人验证失败”,您可能想尝试地址“-f”信封“附加参数:

mail($email_to, $email_subject, $email_message, $headers, "-f $email_from");

邮件功能参考,包括有关“附加参数”的注释,特别是sendmail的-f

http://php.net/manual/en/function.mail.php

  

additional_parameters参数可用于传递其他内容   flags作为配置为使用的程序的命令行选项   发送邮件时,由sendmail_path配置定义   设置。例如,这可用于设置信封发件人   将sendmail与-f sendmail选项一起使用时的地址。

     

应将Web服务器运行的用户添加为受信任的用户   到sendmail配置,以防止'X-Warning'标头   使用时设置信封发件人(-f)时添加到邮件中   这种方法。对于sendmail用户,此文件是/ etc / mail / trusted-users。

答案 1 :(得分:0)

$ email_to ='nobody@example.com';

$ email_subject ='主题';

$ email_message ='hello';

$ headers ='来自:webmaster@example.com'。 “\ r \ n”。

'Reply-To: webmaster@example.com' . "\r\n" .

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

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

试试这个并告诉我