为什么不在php中发送电子邮件


email not send in php why

嗨,我已经创建了一个页面联系我们,但它不起作用,为什么我错了

请帮我

联系页面代码

    <?php
if(isset($_POST['email'])) {
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "azadrohit@gmail.com";
    $email_subject = "Your email subject line";

    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['user_name']) ||
        !isset($_POST['contact_no']) ||
        !isset($_POST['contact_email_id']) ||
        !isset($_POST['city']) ||
        !isset($_POST['project']) ||
        !isset($_POST['local_property']) ||
        !isset($_POST['user_query'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
    $user_name = $_POST['user_name']; // required
    $contact_no = $_POST['contact_no']; // required
    $email_from = $_POST['contact_email_id']; // required
    $city = $_POST['city']; // not required
    $project = $_POST['project']; // not required
    $local_property = $_POST['local_property']; // not required
    $user_query = $_POST['user_query']; // 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,$user_name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$contact_no)) {
    $error_message .= 'The Contact Number you entered does not appear to be valid.<br />';
  }
  if(strlen($user_query) < 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 .= "User Name: ".clean_string($user_name)."'n";
    $email_message .= "Contact No: ".clean_string($contact_no)."'n";
    $email_message .= "Email: ".clean_string($email_from)."'n";
    $email_message .= "City: ".clean_string($city)."'n";
    $email_message .= "Project: ".clean_string($project)."'n";
    $email_message .= "Local Property: ".clean_string($local_property)."'n";
    $email_message .= "User Query: ".clean_string($user_query)."'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); 
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

html代码

<div class="contact_form">
    <p>Quick Contact</p>
    <form action="contactus.php" method="post" name="contactform">
        <label><span>name:-</span> <input type="text" name="user_name"></label>
        <label><span>contact no:-</span> <input type="text" name="contact_no"></label>
        <label><span>email id:-</span> <input type="text" name="contact_email_id"></label>
        <label><span>city:-</span> <input type="text" name="city"></label>
        <label><span>projects:-</span> <input type="text" name="project"></label>
        <label><span>local property:-</span> <input type="text" name="local_property"></label>
        <label><span>query:-</span> <textarea row="" cols="" name="user_query"></textarea></label>
        <input type="submit" value="submit">
    </form>
</div>

将其添加到代码中。。。

    $headers = "From: '"$from_name'" <$from_mail>'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/plain; charset='"utf-8'"'r'n";
$headers .= "Content-Transfer-Encoding: 7bit'r'n";