Php 文件\u使用查询字符串获取\u内容
我正在尝试从php发送电子邮件 我有一个包含所有值的php文件&另一个php模板文件 两个文件都在同一台服务器上 例如,我使用file_get_contents来获取php模板文件的内容Php 文件\u使用查询字符串获取\u内容,php,email,file-get-contents,Php,Email,File Get Contents,我正在尝试从php发送电子邮件 我有一个包含所有值的php文件&另一个php模板文件 两个文件都在同一台服务器上 例如,我使用file_get_contents来获取php模板文件的内容 $url="emil_form.php"; $a="uname"; if(($Content = file_get_contents($url. "?uname=".$a)) === false) { $Content = ""; } ...... EMAIL Sendin
$url="emil_form.php";
$a="uname";
if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
$Content = "";
}
...... EMAIL Sending Code ..........
下面是eml_form.php电子邮件模板文件的代码
Your Name is : <?php $_GET['uname']; ?>
因此,一旦我获得$Content中的数据,我就可以通过电子邮件发送它。
但我无法打开文件时出错
我想要的是将原始php文件中的数据传递到模板php文件中,并将模板的输出存储在变量中,以便通过电子邮件发送
你怎么能做到这一点
谢谢请注意,wss试图加载eml_form.php的$url中存在文件名错误
第二个问题似乎是调用$url时使用的路径。尝试将其设置为一个完整的URL,以便正确解析文件,避免您可能不得不执行的任何模板设置。RTT会比较慢,但您不必更改任何代码。这看起来像:
$url = 'http://www.mysite.com/email_form.php';
$a = "uname";
if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
$Content = "";
}
真正的问题是,您试图读取一个带有http查询字符串的本地文件。文件系统不理解这一点,会查找一个名为eml_form.php?uname=uname的文件。 $\u GET/$\u POST/etc仅在http连接上工作 尝试在模板中放置一个类似%%NAME%%的占位符,并在阅读模板后将其替换
<?php
$url = "emil_form.php";
$a = "uname";
if(($Content = file_get_contents($url)) === false) {
$Content = "";
}
$Content = str_replace('%%NAME%%', $a, $Content);
// sending mail....
这里有一个替代方案。。。 使用cballou建议的相对路径或绝对路径读取文件 但是不要使用一个简单的文本文件来编写php文件,而是将消息模板放入其中,并替换为一些独特的内容,如%uname%替换键。 将文件内容读入变量并用变量替换替换键,如下所示
$url = "/usr/local/path/to/email_form.txt";
$a = "uname";
if(($Content = file_get_contents($url)) === false) {
$Content = "";
}else{
$content = str_replace('%uname%', $a, $content);
}
如果直接调用emil_form.php,会不会出错?uname=uname…?email_form.php和./email_form.php之间有什么区别?第一个答案很好,我刚刚写了同样的东西,但你抢先了我一步。
$url = "/usr/local/path/to/email_form.txt";
$a = "uname";
if(($Content = file_get_contents($url)) === false) {
$Content = "";
}else{
$content = str_replace('%uname%', $a, $content);
}
$url = sprintf("%s://%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']);
$content = file_get_contents($url."/file.php?test=".$test);
echo $content;