2016-04-28 31 views
0

我一直試圖讓我的聯繫表格工作,但我沒有收到我的Gmail郵件文件夾中的任何電子郵件(包括垃圾郵件和垃圾郵件,也無法通過我的任何跟蹤主辦)。聯繫PHP表單 - 不接收郵件(GMAIL)

我一直在關注這個教程:http://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form

需要一些嚴重的幫助,因爲我已經嘗試了一切!

<?php 


$from = $_POST['email']; 
$sendTo = '[email protected]'; 
$subject = 'New message from contact form'; 
$fields = array('name' => 'Name', 'surname' => 'Surname', 'email' =>  'Email', 'phone' => 'Phone', 'message' => 'Message'); 

// array variable name => Text to appear in email 

$okMessage = 'Contact form succesfully submitted. Thank you, I will get back  to you soon!'; 
$errorMessage = 'There was an error while submitting the form. Please try again later'; 

// let's do the sending 

try 
{ 
$emailText = "You have new message from contact  form\n=============================\n"; 

foreach ($_POST as $key => $value) { 

    if (isset($fields[$key])) { 
     $emailText .= "$fields[$key]: $value\n"; 
    } 
} 

    mail($sendTo, $subject, $emailText, "From: " . $from); 

    $responseArray = array('type' => 'success', 'message' => $okMessage); 
} 
catch (\Exception $e) 
{ 
$responseArray = array('type' => 'danger', 'message' => $errorMessage); 
} 

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&  strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
$encoded = json_encode($responseArray); 

header('Content-Type: application/json'); 

echo $encoded; 
} 
else { 
echo $responseArray['message']; 
} 

HTML表單

<form id="contact-form" method="post" action="contact.php" role="form"> 

<div class="messages"></div> 

    <div class="form-group"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-12"> 
       <label>First name*</label> 
       <input type="text" id="form-name" name="name" class="form-  control" placeholder="Please enter your firstname *" required="required"> 
      </div> 
      <div class="col-md-6 col-sm-12"> 
       <label>Last name*</label> 
       <input type="text" name="surname" id="form-surname"  class="form-control" placeholder="Please enter your firstname *"  required="required"> 
      </div> 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-12"> 
       <label>Email*</label> 
       <input type="email" name="email" id="form-email" class="form-control" placeholder="Please enter your firstname *" required="required"> 
      </div> 
      <div class="col-md-6 col-sm-12"> 
       <label>Phone</label> 
       <input type="tel" name="phone" id="form-phone" class="form- control" placeholder="Please enter your phone"> 
      </div> 
     </div> 
    </div> 


    <div class="form-group"> 
     <label for="comment">Message*</label> 
     <textarea class="form-control" rows="7" name="message" id="form-message" default="Type us a message" required="required"></textarea> 
    </div>  

    <div class="checkbox"> 
     <label><input type="checkbox" value="">Join mailing list</label> 
    </div> 
    <input type="submit" class="btm btn-success btn-send" value="Send message"> 
    <p class="text-muted"><strong>*</strong> These fields are required.</p> 
</form> 
+0

你有任何錯誤信息? –

+0

你的主機是否支持php mail()? – AkshayP

+0

據我所知,沒有錯誤消息提交 –

回答

3

由於許多服務器不允許您發送電子郵件在沒有註冊的人的名字,你應該在你的服務器上創建一個電子郵件,它只是用來發送你的用戶在填寫聯繫表單後寫入的電子郵件,然後你應該使用類似的類爲您發送電子郵件。

一個簡單的例子說明如何使用PHPMailer的是:

<?php 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp.yourserver.here'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'password';       // SMTP password 
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587;         // TCP port to connect to 

$mail->setFrom('[email protected]', 'Mailer'); 
$mail->addAddress('[email protected]', 'You');  // Add a recipient 
$mail->addReplyTo('[email protected]', 'User that submitted form'); 

$mail->Subject = 'Subject here'; 
$mail->Body = 'Write here your message'; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 

的例子是從我告訴你上面的鏈接採取。

+0

創建郵件帳戶上傳phpmailer文件並在上面的php代碼中輸入值後,我將如何去測試它? –

+0

你可以把這段代碼放入一個函數中,並在你按下提交按鈕後調用函數 – rafaelcpalmeida