简体   繁体   中英

Post parameters not working in file_get_contents() php

I had two PHP files on my same server, Where one PHP file is used to send the mail and the other PHP file is what will be the body of the other mail so, I do this like this

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){

require_once('connect.php');

$email = $_POST['email'];
$name = $_POST['name'];
print_r($email);
print_r($name);

$postdata = http_build_query(
array(
    'email' => $email,
    'name' => $name
)
);

$opts = array('http' =>
array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postdata
 )
 );

$to=$email;
$subject="Welcome Aboard| Judgement6";
$context = stream_context_create($opts);
$email_text = file_get_contents('Judgement6.php',false,$context);

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Judgement6 fantasy game<xyz@gmail.com>" . "\r\n";


if(mail($to,$subject,$email_text,$headers))
{
echo 'email sent';
}
else{
echo 'email not sent';
}
  }

   ?>

Now the problem is that the file is included in the body of my mail but the post parameters never went there and the required variables remain null in the second file...

file_get_contents() returns the file in a string as it is, it will not pass any existing variables, for that you will need to use include or require.

http://php.net/manual/en/function.file-get-contents.php

What you can do is inside Judgement6.php create the variable $email_text and then include the file in your script.

Inside Judgement6.php:

$mail_text = "ALL THE CONTENT AND $VARIABLES INSIDE Judgement6.php";

For example, if Judgement6.php has the following script:

Hello <?php echo $name; ?>,
Thank you for subscribing to our <?php echo $_POST['service']; ?> 
on <?php echo date("Y-m-d"); ?>.

You will write

$mail_text = "Hello  $name,
Thank you for subscribing to our ".$_POST['service']." 
on ".date("Y-m-d").".";

Be careful with concatenation and using " inside the string, you will need to escape them \\"

In your file


$to=$email;
$subject="Welcome Aboard| Judgement6";    
include_once('Judgement6.php');

or


$to=$email;
$subject="Welcome Aboard| Judgement6";  
require_once('Judgement6.php');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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