dtdfl62844
2019-05-03 08:11 浏览 127
已采纳

POST请求空白,为什么?

I have the following code saved as send.php:

<?php
$to_email = "person@gmail.com";
$subject = $_POST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me@myWebsite.com";

if ( mail($to_email, $subject, $body, $headers)) {
    echo("Email successfully sent to $to_email...");
} else {
    echo("Email sending failed...");
}
?>

The email sends if I navigate to https://myWebsite.com/Stuff/send.php and make $subject = "something"

But if I set $subject to be a POST variable. It does not send the subject when I write this URL in the browser (as in, the email still sends but the subject is now blank):

 https://myWebsite.com/Stuff/send.php?subject=notworking

I've been racking my head with this problem and have been struggling for a couple hours. Anyone know?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答 默认 最新

  • 已采纳
    doumixiang2227 doumixiang2227 2019-05-03 08:15

    To get both $_POST and $_GET you can use $_REQUEST which will work for both.

    In your code replace

    $subject = $_GET['subject'];
    

    With

    $subject = $_REQUEST['subject'];
    

    If someone direct hit the URL in browser without paramter subject you can prevent them using

    if(!empty($_REQUEST['subject']))
    {
      $to_email = "person@gmail.com";
      $subject = $_REQUEST['subject'];
      $body = "Hi, This is test email send by PHP Script";
      $headers = "From: Me@myWebsite.com";
    
      if ( mail($to_email, $subject, $body, $headers))
        echo("Email successfully sent to $to_email...");
      else
        echo("Email sending failed...");
    }
    
    点赞 评论 复制链接分享
  • duanlie7447 duanlie7447 2019-05-03 08:14

    If you want to extract the values from the URL parameters then you need to use the $_GET global variable to get the data like this,

    <?php
    $to_email = "person@gmail.com";
    $subject = $_GET['subject'];
    $body = "Hi, This is test email send by PHP Script";
    $headers = "From: Me@myWebsite.com";
    
    if ( mail($to_email, $subject, $body, $headers)) {
        echo("Email successfully sent to $to_email...");
    } else {
        echo("Email sending failed...");
    }
    ?>
    
    点赞 评论 复制链接分享
  • doujing2497 doujing2497 2019-05-03 08:15

    Try it.

     <?php
        $to_email = "person@gmail.com";
        $subject = (isset($_GET['subject']) && (!empty($_GET['subject']))?$_GET['subject']:"No Subject";
        $body = "Hi, This is test email send by PHP Script";
        $headers = "From: Me@myWebsite.com";
    
        if ( mail($to_email, $subject, $body, $headers))
            echo("Email successfully sent to $to_email...");
        else
            echo("Email sending failed...");
    ?>
    
    点赞 评论 复制链接分享

相关推荐