【问题标题】:Adding an external mesage to php sendmail向 php sendmail 添加外部消息
【发布时间】:2018-12-03 14:50:14
【问题描述】:

这一定是一个简单而愚蠢的问题,但似乎无法弄清楚。 我想做的是:

$message = include ('./myfile.php');

显然这样是行不通的,但是如果该代码有效的话,有没有办法做到这一点。

感谢

这里是完整的代码(缩写):

        require_once ('./connect.php');
    $db = mysqli_connect($db_hostname,$db_username,$db_password,"paratb_members");
    $result = mysqli_query($db,"SELECT * FROM board where accesskey = 'CHHXN5Jdwu'");
    while ($row = mysqli_fetch_array($result)) {
    extract($row);
    $message = include './questions/resignation.php'; 

    $subject = "IAP Ballot";    
    $headers = "From: xxxx" . "\r\n";
    $headers .= "Reply-To: xxxx" . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $fromEmail = "xxxx";
    $fifth = "-f" . $fromEmail;
    $to      = "$fname $lname <$email>";
    mail($to, $subject, $message, $headers, $fifth);    
    echo "Email sent to $lname: $email<br>";

【问题讨论】:

  • 您的问题没有提供有关您遇到的问题的明确信息。您想要实现的目标有点混乱。
  • myfile.php 中有什么您希望最终出现在变量中的内容?
  • “显然这样行不通” - 当然可以,只要 myfile.php 有一个 return 语句返回您希望变量包含的内容。查看手册中的“示例 #5”:php.net/manual/en/function.include.php
  • 实际上它不起作用。如果 myfile.php 实际上是电子邮件消息,则 $message 会打印在浏览器中,但不会打印在电子邮件中。还尝试使用包含具有相同结果的 $message 语句的普通包含。 myfile.php 确实包含变量。

标签: php email sendmail


【解决方案1】:

您可以先读取文件并将数据存储在包含文件数据的变量中,然后您可以使用该变量。

fopen("myfile.php", "r");
    $lines = file("myfile.php");
    $message = '';
    foreach($lines as $value) {
        $message .= $value." ";
    }
    echo ($message);

【讨论】:

  • 一个问题。如果该文件包含任何 PHP,它将永远不会被执行,而是被当作纯文本读取。此外,如果您只想要文件的内容,只需使用 file_get_contents() 而不是像那样遍历结果可能会更容易。它会将上述代码从 7 行减少到 1 行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2018-08-29
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多