网络响应 200,但收到错误消息;PHP 发送电子邮件脚本

我在某个地方得到了这个 PHP 代码,一切看起来都还不错,但是每次我尝试发送测试电子邮件时都会收到一条错误消息;但是,网络消息说它确实发送了。请查看图片和代码片段。请帮忙。谢谢


PHP 脚本




    session_cache_limiter( 'nocache' );

    header( 'Expires: ' . gmdate( 'r', 0 ) );

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


    $to         = 'myemail@gmail.com';  // put your email here


    $email_template = 'simple.html';


    $subject    = strip_tags($_POST['subject']);

    $email       = strip_tags($_POST['email']);

    $phone      = strip_tags($_POST['phone']);

    $name       = strip_tags($_POST['name']);

    $message    = nl2br( htmlspecialchars($_POST['message'], ENT_QUOTES) );

    $result     = array();



    if(empty($name)){


        $result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );

        echo json_encode($result );

        die;

    } 


    if(empty($email)){


        $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );

        echo json_encode($result );

        die;

    } 


    if(empty($message)){


         $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );

         echo json_encode($result );

         die;

    }




    $headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";

    $headers .= "Reply-To: ". $email . "\r\n";

    $headers .= "MIME-Version: 1.0\r\n";

    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";



    $templateTags =  array(

        '{{subject}}' => $subject,

        '{{email}}'=>$email,

        '{{phone}}'=>$phone,

        '{{name}}'=>$name,

        '{{message}}'=>$message

        );

HTML FORM - 这是我的 HTML 代码。我的表格可能有问题吗?然而,我认为这是 PHP,或者某些不匹配的东西。


慕森王
浏览 123回答 2
2回答

拉莫斯之舞

您收到 200 错误的原因是您自己捕获了错误并回显了输出。如果您希望错误为 500,则可以尝试在错误中添加此 if/else:header($_SERVER["SERVER_PROTOCOL"] . ' 500 Internal Server Error', true, 500);&nbsp;&nbsp;exit;为了从邮件函数本身调试错误,这篇文章解释了:$success = mail('example@example.com', 'My Subject', $message);if (!$success) {&nbsp; &nbsp; $errorMessage = error_get_last()['message'];}

qq_笑_17

您的脚本响应 HTTP 200,错误导致邮件功能不成功。尝试替换行:$result&nbsp;=&nbsp;array(&nbsp;'response'&nbsp;=>&nbsp;'error',&nbsp;'message'=>'<strong>Error!</strong>&nbsp;&nbsp;Cann\'t&nbsp;Send&nbsp;Mail.'&nbsp;&nbsp;);和:$result&nbsp;=&nbsp;array(&nbsp;'response'&nbsp;=>&nbsp;'error',&nbsp;'message'=>'<strong>Error!</strong>&nbsp;&nbsp;Cann\'t&nbsp;Send&nbsp;Mail.',&nbsp;'reason'&nbsp;=>&nbsp;error_get_last()['message']&nbsp;);添加到数组中,会显示什么错误'reason'&nbsp;=>&nbsp;error_get_last()['message']你会看到为什么你的邮件没有发送。我希望它对你有帮助:)
打开App,查看更多内容
随时随地看视频慕课网APP