2016-06-27 31 views
0

我正在用php郵件腳本構建基於html5的聯繫表單。我收到我的網站上的空電子郵件

我不知道爲什麼我會在提交時收到空電子郵件以及如果我想打印任何消息,如「謝謝您與我們聯繫」,我該怎麼做。

<?php 
$name = (isset($_POST['name']) ? $_POST['name'] : null); 
$phone = (isset($_POST['phone']) ? $_POST['phone'] : null); 
$address = (isset($_POST['address']) ? $_POST['address'] : null); 
$appointment_datepicker =(isset($_POST['appointment-datepicker']) ? $_POST['appointment-datepicker'] : null); 
$message = (isset($_POST['textarea']) ? $_POST['textarea'] : null); 
$email_address = (isset($_POST['email']) ? $_POST['email'] : null); 



// 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\nPhone: $phone\n\nEmail: $email_address\n\nAppointment: $appointment_datepicker\n\nAddress: $address\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_status = mail($to,$email_subject,$email_body,$headers); 

/** Check for empty fields 
if(!empty($_POST['name']) || !empty($_POST['phone']) || !empty($_POST['address']) || !empty($_POST['appointment-datepicker']) || !empty($_POST['textarea']) || filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) 
{**/ 
if ($mail_status) { ?> 
    <script language="javascript" type="text/javascript"> 
     print("Thank you for the message. We will contact you shortly."); 
     window.location = 'http://thankyou.html'; 
    </script> 
    <?php 
    } 

else { ?> 
    <script language="javascript" type="text/javascript"> 
     print('Message failed. Please, send an email to [email protected]'); 
     window.location = 'http://thankyou.html'; 
    </script> 
<?php 
} 
?> 

<form id="appointment-form" action="contact_me.php" method="post" class="appointment-form"> 
            <div class="col-md-6"> 
             <div class="form-group form-md-line-input form-md-floating-label"> 
              <input id="name" type="text" class="form-control" required="required"> 
              <label for="name">Name</label> 
             </div> 
             <div class="form-group form-md-line-input form-md-floating-label"> 
              <input id="phone" type="text" class="form-control" required="required" pattern="/[1-9][01][0-9]-?[0-9]{3}-=[0-9]{4}"> 
              <label for="phone">Phone Number</label> 
             </div> 
             <div class="form-group form-md-line-input form-md-floating-label"> 
              <input id="address" type="text" class="form-control" required="required"> 
              <label for="address">Address </label> 
             </div> 
             <div class="form-group form-md-line-input form-md-floating-label"> 
              <input id="appointment-datepicker" type="text" class="form-control form-line-input" required="required"> 
              <label for="appointment-datepicker">Book a date</label> 
             </div> 
            </div> 
            <div class="col-md-6"> 
             <div class="form-group form-md-line-input form-md-floating-label"> 
              <input id="email" type="text" class="form-control" required="required" pattern="[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~.-][email protected][a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*"> 
              <label for="email">Email Address</label> 
             </div> 
             <div class="form-group form-md-line-input form-md-floating-label"> 
              <textarea id="textarea" rows="4" class="form-control form-textarea"></textarea> 
              <label for="textarea">Message</label> 
             </div> 
             <div class="btn-wrapper"> 
              <button type="submit" class="btn btn-make-app">Send to us</button> 
             </div> 
            </div> 
            <div class="clearfix"> </div> 
           </form> 
+1

您的郵件和標頭格式錯誤,容易受到標頭注入攻擊。請勿直接使用郵件,請使用庫來正確執行此操作,例如[PHPMailer](https://github.com/PHPMailer/PHPMailer),您使用此標記標記此問題。 – Synchro

+0

嗨Synchro,我應該做什麼? – user3377410

+0

點擊鏈接? – Synchro

回答

0

您的表單輸入缺少「名稱」屬性。 ID不會在表單請求中傳遞;只有名稱 - 值配對。

    v---------v 
<input id="name" name="name" type="text" class="form-control" required="required"> 
+0

謝謝。讓我試試這個。 – user3377410

相關問題