2014-12-13 112 views
2

我已經下載了一個Bootstrap網站模板,並試圖將發送到我的電子郵件地址的表單工作。它提供了當填寫所有評論時都提交的消息,但沒有發送實際消息的消息。HTML電子郵件表格不發送郵件

下面是從指數的HTML塊:

<section id="contact"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-lg-12 text-center"> 
       <h2>Contact Us</h2> 
       <hr class="star-primary"> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="col-lg-8 col-lg-offset-2"> 
       <!-- To configure the contact form email address, go to mail/contact_me.php and update the email address in the PHP file on line 19. --> 
       <!-- The form should work on most web servers, but if the form is not working you may need to configure your web server differently. --> 
       <form name="sentMessage" id="contactForm" novalidate> 
        <div class="row control-group"> 
         <div class="form-group col-xs-12 floating-label-form-group controls"> 
          <label>Name</label> 
          <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name."> 
          <p class="help-block text-danger"></p> 
         </div> 
        </div> 
        <div class="row control-group"> 
         <div class="form-group col-xs-12 floating-label-form-group controls"> 
          <label>Email Address</label> 
          <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address."> 
          <p class="help-block text-danger"></p> 
         </div> 
        </div> 
        <div class="row control-group"> 
         <div class="form-group col-xs-12 floating-label-form-group controls"> 
          <label>Phone Number</label> 
          <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number."> 
          <p class="help-block text-danger"></p> 
         </div> 
        </div> 
        <div class="row control-group"> 
         <div class="form-group col-xs-12 floating-label-form-group controls"> 
          <label>Message</label> 
          <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea> 
          <p class="help-block text-danger"></p> 
         </div> 
        </div> 
        <br> 
        <div id="success"></div> 
        <div class="row"> 
         <div class="form-group col-xs-12"> 
          <button type="submit" class="btn btn-success btn-lg" action="public_html/mail/contact_me.php">Send</button> 
         </div> 
        </div> 
       </form> 
      </div> 
     </div> 
    </div> 
</section> 

下面是來自PHP文件中的代碼:

<?php 
// Check for empty fields 
if(empty($_POST['name'])  || 
    empty($_POST['email'])  || 
    empty($_POST['phone'])  || 
    empty($_POST['message']) || 
    !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) 
    { 
    echo "No arguments Provided!"; 
    return false; 
    } 

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$phone = $_POST['phone']; 
$message = $_POST['message']; 

// Create the email and send the message 
$to = '[email protected]'; // Add your email address inbetween the '' replacing    
[email protected] - This is where the form will send a message to. 
$email_subject = "Website Contact Form: $name"; 
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the   
details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message"; 
$headers = "From: [email protected]\n"; // This is the email address the generated message will  
be from. We recommend using something like [email protected] 
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers); 
return true;    
?> 

請幫幫忙!

預先感謝您

+1

從我所看到的,您的表單輸入沒有名稱。 – Rasclatt 2014-12-13 03:01:47

+0

另外,您正在檢查'$ _POST',但您可能正在執行'$ _GET' – Rasclatt 2014-12-13 03:03:00

回答

1

你的表單沒有方法,所以可能它做的$_POST$_GET代替:

<form name="sentMessage" id="contactForm" novalidate method="post" action="public_html/mail/contact_me.php"> 

您對您輸入的任何名稱(name=""):

<input name="email" type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address."> 

此外,你可以打破你的劇本好一點:

function Validate() 
    { 
     // Check for empty fields 
     if(empty($_POST['name'])   || 
       empty($_POST['email'])  || 
       empty($_POST['phone'])  || 
       empty($_POST['message']) || 
       !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) { 

       return false; 
      } 
     else 
      return true; 
    } 

function SendEmail($to = '[email protected]') 
    { 
     if(Validate() == true) { 
       $name   = $_POST['name']; 
       $email_address = $_POST['email']; 
       $phone   = $_POST['phone']; 
       $message  = $_POST['message']; 

       // Create the email and send the message 
       $email_subject = "Website Contact Form: $name"; 
       $email_body  = "You have received a new message from your website contact form.\n\n"."Here are the   
       details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message"; 
       $headers  = "From: [email protected]\n"; 
       $headers  .= "Reply-To: $email_address"; 
       // Send true on successful send. 
       // Send false if failed 
       return (mail($to,$email_subject,$email_body,$headers))? true: false; 
      } 
     else 
      // Invalid inputs 
      return 'err'; 
    } 

    // Apply function(s). You will get true, false, or err 
    $send = SendEmail(); 

    // On return, you can echo any result 
    if($send == 'err') 
     echo 'Invalid Fields.'; 
    elseif($send == false) 
     echo 'An Error Occurred.'; 
    else 
     echo 'Email Sent Successfully.'; 
0

你確定它調用你的php文件嗎?我會將您的按鈕上的action="public_html/mail/contact_me.php移動到開頭的form標記。

你也可以在php文件的各個位置放一些「調試」代碼來幫助排除故障。

例如,您的託管服務器上可能未啓用mail()功能。您可以在郵件行前面輸入console.log('About to send email'),在郵件行後面輸入console.log('Mail sent')。這將出現在Firebug例如,並幫助您看看腳本是否正常工作。

您還表示說明它已成功提交,但我沒有看到任何代碼可以輸出成功消息。你能更清楚地告訴你如何被通知它是「成功」的嗎?

0

嘗試這樣做<form name="sentMessage" id="contactForm" method='post' action='public_html/mail/contact_me.php' novalidate><form>注:如果輸入字段沒有name='email'

0

把目的地表單標籤$_POST['name']將無法​​正常工作。

<form action='contact_me.php'> 
<input type='submit'>