繁体   English   中英

如何从另一个文件中获取 PHP 变量?

[英]How to get PHP variable from another file?

我正在尝试在 xampp 上使用 PHPMailer 制作一个忘记密码系统。 但是当我发送电子邮件时,电子邮件正文是一个 html 邮件模板,我从另一个文件$mail->Body = file_get_contents('mail_template.php'); ,而且我有问题,因为我不知道应该如何获取$url变量的数据并将其发送到mail_template.php文件以将其用于邮件中的链接。 我试过include 'filename.php'; 命令,但没有工作。

这是代码:

if (isset($_POST["submitButton"])) {
    $emailTo = $_POST["email"];

    $code = md5(uniqid(rand(), true));
    $query = $con->prepare("INSERT INTO resetpasswords(code,email) VALUES('$code', '$emailTo')");
    $query->execute();
    if (!$query) {
        exit("Something went wrong...");
    }

    $mail = new PHPMailer(true);

    try {
        //Server settings                                           // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host = 'smtp.gmail.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth = true;                                   // Enable SMTP authentication
        $mail->Username = 'mail@gmail.com';                     // SMTP username
        $mail->Password = 'password';                               // SMTP password
        $mail->SMTPSecure = 'ssl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port = 465;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        //Recipients
        $mail->setFrom('mail@mail.com', 'Email');
        $mail->addAddress($emailTo);     // Add a recipient
        $mail->addReplyTo('no-reply@website.com', 'No reply');


        // Content
        $url = "http://" . $_SERVER["HTTP_HOST"] . dirname($_SERVER["PHP_SELF"]) . "/resetPassword/code/$code";
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Reset your password!';
        $mail->Body = file_get_contents('mail_template.php');
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
    } catch (Exception $e) {
        echo "Message could not be sent. Error: {$mail->ErrorInfo}";
    }
    header("refresh:10;url=login.php");
}

所以从这个 php 文件中,我想将$url的数据发送到mail_template.php

这是可能的吗?

要将数据发送到 php 中的另一个 url,您必须尝试以下 url:

`mail_template.php?data=mail@gmail.com` 

然后在一个空的 mail_template.php 里面做 -

 <?php 
    if (isset($_GET['data'])) {
      $test = $_GET['data'];
//print the data added to the url
      echo $test;
    }
    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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