phpMailer脚本无法正常工作

时间:2015-12-09 15:30:19

标签: php html forms mailer

有人可以告诉我为什么我的邮件脚本无法正常运行吗?我希望我的html表单提交并发送内容到 - hello@weblabcompany.com。

index.html和mail.php存储在我的网站根文件夹

以下代码

<form>
    <form action="/mail.php" method="post">
    <fieldset>
        <div class="col-2 pblue"> <input type="text" name="name" value="Name" class="width-80"> </div>
        <div class="col-2 pblue"> <input type="email" name="email" value="Email" class="width-80"> </div>
        <div class="col-2 pblue"> 
            <select name="size" class="width-80"> 
            <option value"" disabled selected> Business Size</option>
            <option value="saab">Sole Trader/Freelancer</option>
            <option value="mercedes">Start Up</option>
            <option value="audi">Small Business</option>
        </select> 
        </div>
        <div class="col-2 pblue"> 
            <select name="budget" class="width-80"> 
            <option value"" disabled selected>Budget</option>
            <option value="500-1000">£500-1000</option>
            <option value="1000-2000">£1000-2000</option>
            <option value="1000-2000">£2000-3000</option>
            <option value="3000-5000">£3000-5000</option>
        </select> 
        </div>
        <div class="col-1 pblue"> <textarea name="message" placeholder="How can we help?" cols="25" rows="4" value="How can we help" class="width-90 height"> </textarea>
        </div>
        <div class="col-1 pblue"> <input type="submit" value="Send" class="width-25"> </div> 
    </fieldset>
</form>

<?php

if(isset($_POST['email'])) {


   // EDIT THE 2 LINES BELOW AS REQUIRED

   $email_to = "hello@weblabcompany.com";

   $email_subject = "New Enquiry";

   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();
   }


    if(!isset($_POST['Name']) ||
       !isset($_POST['Email']) ||
       !isset($_POST['Size']) ||
       !isset($_POST['Budget']) ||
       !isset($_POST['Message'])) {

          died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST['Name']; // required
    $email = $_POST['Email']; // required
    $size = $_POST['Size']; // required
    $telephone = $_POST['Budget']; // not required    
    $comments = $_POST['Size']; // 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 .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Size: ".clean_string($size)."\n";
    $email_message .= "Budget: ".clean_string($budget)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

    $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
}
?>

0 个答案:

没有答案