发布参数在file_get_contents()PHP中不起作用
我在同一台服务器上有两个PHP文件,其中一个PHP文件用于发送邮件,而另一个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';
}
}
?>
现在的问题是该文件包含在我的邮件正文中,但post参数从未消失在那里,所需的变量在第二个文件中保持为空...
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()
照原样以字符串形式返回文件,它不会传递任何现有变量,因此您将需要使用include或require。
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
您可以在Judgement6.php中创建变量 $ email_text
然后将文件包含在脚本中。
What you can do is inside Judgement6.php create the variable $email_text
and then include the file in your script.
内部Judgement6.php:
Inside Judgement6.php:
$mail_text = "ALL THE CONTENT AND $VARIABLES INSIDE Judgement6.php";
例如,如果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"); ?>.
您将写
$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 \"
在文件中
$to=$email;
$subject="Welcome Aboard| Judgement6";
include_once('Judgement6.php');
或
or
$to=$email;
$subject="Welcome Aboard| Judgement6";
require_once('Judgement6.php');