php发送电子邮件脚本

时间:2014-02-27 21:26:08

标签: php forms email

我有一个PHP脚本和一个简单的表单,使用此脚本我没有收到任何PHP错误,它在提交表单后生成我的成功html,但没有电子邮件发送到该地址。

这是我的表单代码:

      <form role="form" name="contact_form" id="contact_form" action="process_mail.php" method="POST">
        <div class="editable_form">
        <div class="form-group">
          <input type="text" class="form-control" name="name" id="name" placeholder="Name" title="Name">
          <br/>
          <input type="email" class="form-control" id="" placeholder="Reply email address" name="email">
           <br/>
          <textarea class="form-control" rows="3" name="comments" id="comments"></textarea>
        </div>
        <center><button type="submit" class="btn btn-success">Submit</button></center>
        </div>
      </form>  

这是PHP脚本:

<?php
error_reporting(E_ALL); ini_set('display_errors', 'On'); 
if(isset($_POST['email'])) {
$email_to = "MY EMAIL ADDRESS";
$email_subject = "SUBJECT";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$name = $_POST['name']; // required
$email_from = $_POST['email']; // 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,$name)) {
  $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\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); 
?>

0 个答案:

没有答案
相关问题