在使用PHPMailer发送电子邮件时,是什么原因导致$_POST变量解析为空?[已关闭]

t9aqgxwy  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(107)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
3个月前关闭。
此帖子已于11天前编辑并提交审核,未能重新打开帖子:
原始关闭原因未解决
Improve this question
我有一个简单表格:

<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 PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 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变量解析为空?

yks3o0rb

yks3o0rb1#

@neuticle的答案解决了这个问题
删除行:

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

完全。这是不必要的,并且会覆盖$_POST超级全局”

相关问题