在使用PHPMailer发送电子邮件时,是什么使$_POST变量解析为空



我有一个简单的形式:

<form method="post" action="/inc/contact.php"   id="contact-form" >         
<div class="messages"></div>
<div>
<div>
<div>
<label>Name*</label>
<input type="text" placeholder="John Doe" name="name"
required="required" data-error="Name is required."> 
<div ></div>
</div>
</div>
<div>
<div >
<label>Email*</label>
<input type="email" placeholder="john.doe@mybusiness.com" 
name="email" required="required" data-error="Valid email is          
required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div>
<div >
<textarea placeholder="Your message*" name="message" 
required="required" data-error="Please,leave us a message."></textarea>
<div></div>
</div>
</div>
<div ><button type="submit" name="submit">Send Message</button></div>
</div>
</form>

它将数据发送到contact.php文件。我正在使用MailerPHP发送电子邮件。邮件程序运行良好,我已经在没有的情况下验证了这一点

if(empty($_POST['submit'])) {

声明。如果我在If语句中包含所有内容,则不会发送任何内容。看起来$_POST变量确实为空。

<?php
// This example shows making an SMTP connection with authentication.

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$_POST = json_decode(file_get_contents('php://input'), true);
if(!empty($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->Host = 'host';
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'uname';
//Password to use for SMTP authentication
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Set who the message is to be sent from
$mail->setFrom('xxx@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('xxx@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('xxx@example.com', 'John Doe');
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body .= "this is a message" . $_POST['message'] ;
try {
$mail->send();
echo 'Your message was sent successfully!';
} catch (Exception $e) {
echo "Your message could not be sent!";

}
} else {
echo "There is a problem with the contact.html
document!";             
}

我的开发人员工具的"网络响应"选项卡看起来已填充

{
"name": "sdsdsd",
"email": "xxx@example.com",
"message": "kjlhjkuhkkhm",
"submit": ""
}

什么可以使$_POST变量解析为空?

@neuticle的答案解决了这个

删除行:

$_POST =json_decode(file_get_contents('php://input'), true);  

完全。这是不必要的,并且覆盖$_POST superglobal";

最新更新