2017-07-20 118 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代码示例。