我在同一台服务器上有两个PHP文件,其中一个PHP文件用于发送邮件,另一个PHP文件将作为另一封邮件的正文,因此,我这样做是
<?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参数从未到过那里,并且所需的变量在第二个文件中仍然为空...
答案 0 :(得分:0)
file_get_contents()
以字符串形式返回文件,它不会传递任何现有变量,因为您需要使用include或require。
http://php.net/manual/en/function.file-get-contents.php
您可以在Judgement6.php中创建变量$email_text
,然后在脚本中包含文件。
Judgement6.php内部:
$mail_text = "ALL THE CONTENT AND $VARIABLES INSIDE Judgement6.php";
例如,如果Judgement6.php具有以下脚本:
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").".";
在连接时要小心,并在字符串中使用"
,您将需要对其进行转义\"
在您的文件中
$to=$email;
$subject="Welcome Aboard| Judgement6";
include_once('Judgement6.php');
或
$to=$email;
$subject="Welcome Aboard| Judgement6";
require_once('Judgement6.php');