简体   繁体   English

接收空白页面,提交php联系表单时没有错误

[英]Receiving a blank page with no error on submitting php contact form

I made a simple Contact us form and it worked perfectly fine. 我做了一个简单的“联系我们”表格,效果很好。 But now, after adding a mailgun script for sending and receiving emails it is showing me nothing but a blank page . 但是现在,在添加了用于发送和接收电子邮件的mailgun脚本后,除了空白页面外,什么都没有显示。

Script: 脚本:

<?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 <test@example.com >';
$to = 'Demo contact form <test@example.com>';
$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 <postmaster@sandbox614b.mailgun.org>',
                'to'      => 'Nami <nami19@gmail.com>',
                '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);
}

?>

What could I be possibly doing wrong? 我可能做错了什么?

To be frank, it looks like a lot has gone wrong with that code. 坦率地说,该代码似乎出了很多问题。 Unless of course there is code you haven't posted, most of that code doesn't appear to actually do anything or is otherwise connected to anything. 除非当然没有您未发布的代码,否则大多数代码似乎实际上并未执行任何操作或以其他方式与任何内容连接。 What you've posted is only 57 lines of code, so line 68 doesn't exist so far as I am concerned (in regards to your other comment). 您发布的内容只有57行代码,因此就我而言,第68行并不存在(关于您的其他评论)。

You've got a syntax error in your sendMessage() function, as well as where you define your $content variable. 您的sendMessage()函数以及定义$ content变量的位置都有语法错误。 You are declaring $mailgun as a class but don't actually use it anywhere. 您将$ mailgun声明为一类,但实际上没有在任何地方使用它。 Your forEach() loop iterates through each post value and tries to send an email, which in your case, I don't believe is the goal. 您的forEach()循环遍历每个post值并尝试发送电子邮件,对于您而言,我认为这不是目标。 Your if statement within that for loop will always return true and as such, there is no point in using it. 您在该for循环中的if语句将始终返回true,因此使用它毫无意义。 These are just the things that immediately stick out to me. 这些只是立即暴露给我的东西。

Based on what you've posted, this is the only code that appears to have logical relevance: 根据您发布的内容,这是唯一看起来具有逻辑相关性的代码:

<?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 <postmaster@sandbox614b.mailgun.org>",
        'to'      => "Nami <nami19@gmail.com>",
        '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);

}

?>

I suggest you compare the two and spot some of the key differences to start. 我建议您将两者进行比较,并找出一些关键的差异。

Namita, this is a wonderful community full of those who wish to help you. Namita,这是一个很棒的社区,到处都是希望帮助您的人。 However, your question is too general. 但是,您的问题太笼统了。 It looks as though you've pasted bits and pieces from random places without much thought to what the code does. 似乎您是从随机位置粘贴了零碎的片段而没有过多考虑代码的作用。 Moreover, and more importantly, you haven't provided us with an actual error of any kind nor anything you've done to try and fix the issue, let alone even identify the issue. 而且,更重要的是,您没有向我们提供任何形式的实际错误,也没有为解决问题而做的任何事情,更不用说确定问题了。

I recommend you take a look at the MailGun documentation which you can find here: https://documentation.mailgun.com/en/latest/user_manual.html 我建议您看一下MailGun文档,您可以在这里找到: https ://documentation.mailgun.com/en/latest/user_manual.html

Be sure to choose "PHP" from the bar at the top so as to see PHP code examples. 请确保从顶部的栏中选择“ PHP”,以查看PHP代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM