2017-07-20 116 views
1

我做了一個簡單的聯繫我們表單,它工作得很好。但是現在,在添加用於發送和接收電子郵件的mailgun腳本之後,它只顯示了一個空白頁面。在提交php聯繫表格時收到空白頁面沒有錯誤

腳本:

<?php 
require 'vendor/autoload.php'; 
use Mailgun\Mailgun; 
$mailgun = new Mailgun('key-41616099541fe1b0187e7cd970127240', new \Http\Adapter\Guzzle6\Client()); 

$mgClient = new Mailgun('key-41616099'); 
$domain = "sandbox614b.mailgun.org"; 

$from = 'Demo contact form <[email protected] >'; 
$to = 'Demo contact form <[email protected]>'; 
$name = $_POST['name']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 

$fields = array('name' => 'Name', 'email' => 'Email', 'phone' => 'Phone', 'subject' => 'Subject', 'message' => 'Message'); 

$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!'; 

$errorMessage = 'There was an error while submitting the form. Please try again later'; 

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0); 
error_reporting(E_ALL & ~E_NOTICE); 
try 
{ 
    if(count($_POST) == 0) throw new \Exception('Form is empty'); 

    $content = "You have a new message from your contact form"; 
    foreach ($_POST as $key => $value) 
    { 
     if (isset($fields[$key])) 
      { 
      $content .= "$fields[$key]: $value\n"; 

    //recaptcha-response 
    $recaptcha_secret = "6LfK7ygUAAAAAIYzE6mbqdxbmuroi4gJWqdIpmBu"; 
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']); 
    $response = json_decode($response, true); 

# Make the call to the client. 
$result = $mgClient->sendMessage("$domain", 
      array('from' => 'Mailgun Sandbox <[email protected]>', 
       'to'  => 'Nami <[email protected]>', 
       'subject' => 'Hello Nami', 
       'text' => 'Congratulations Nami, you just sent an email with Mailgun! You are truly awesome! ')); 

     echo "Form Submit Successfully."; 
     } else { 
     echo "You are a robot"; 
     } 
    } 
} 
    catch (\Exception $e) 
{ 
    $responseArray = array('type' => 'danger', 'message' => $errorMessage); 
} 

?> 

我應該怎麼做來可能做錯了什麼?

+0

檢查您的錯誤日誌中是否有任何內容 – Kai

+0

我收到的最後一個錯誤日誌是這樣的:語法錯誤,第68行中意外的'else'(T_ELSE),但我確實檢查了大括號並計算了錯誤。現在,當我運行表單時,它顯示空白頁面。 –

回答

0

坦率地說,它看起來好像很多代碼出了問題。除非你有沒有發佈代碼,否則大部分代碼看起來並沒有實際做任何事情或者與其他任何東西有關。你發佈的只有57行代碼,所以第68行根本不存在(關於你的其他評論)。

你有一個語法錯誤在你的sendMessage()功能,以及在此定義您的$內容變量。你聲明$ mailgun作爲一個類,但實際上並沒有在任何地方使用它。您的forEach()循環遍歷每個帖子值並嘗試發送一封電子郵件,在您的情況下,我不相信這是目標。你在if循環中的if語句總是返回true,因此使用它沒有意義。這些只是立即伸出我的東西。

根據您所張貼的東西,這是一個看起來有邏輯關聯的唯一代碼:

<?php 

require 'vendor/autoload.php'; 
use Mailgun\Mailgun; 

$mgClient = new Mailgun('key-41616099'); 
$domain = "sandbox614b.mailgun.org"; 

$name = $_POST['name']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 

$errorMessage = 'There was an error while submitting the form. Please try again later'; 

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0); 
error_reporting(E_ALL & ~E_NOTICE); 

try { 

    if(count($_POST) == 0) { 

     throw new \Exception('Form is empty'); 

    } 

    $data = array(
     'from' => "Mailgun Sandbox <[email protected]>", 
     'to'  => "Nami <[email protected]>", 
     'subject' => $subject, 
     'text' => "Congratulations ".$name.",\n".$message 
    ); 

    # Make the call to the client. 
    $result = $mgClient->sendMessage($domain, $data); 

    echo "Form Submit Successfully."; 

} 

catch (\Exception $e) { 

    $responseArray = array('type' => 'danger', 'message' => $errorMessage); 

} 

?> 

我建議你比較兩個和現場的一些關鍵分歧開始。

Namita,這是一個很棒的社區,其中有很多人希望幫助你。但是,你的問題太籠統了。看起來好像你已經從隨機的地方粘貼了一些零碎的東西,而沒有多少想到代碼的作用。此外,更重要的是,您沒有向我們提供任何類型的實際錯誤,也沒有提供任何您嘗試解決問題的方法,更不用說甚至找出問題了。

我建議你看看MailGun文檔,你可以在這裏找到:https://documentation.mailgun.com/en/latest/user_manual.html

確保從頂部的欄中選擇「PHP」,以便看到PHP代碼示例。