0

I'm trying to make a forgot password system with PHPMailer on xampp. But when I send an email,the email body is a html mail template,what I get from another file $mail->Body = file_get_contents('mail_template.php');,and I have problem,because I don't know how should I get the data of the $url variable and send it to mail_template.php file to use it for the link in the mail. I tried the include 'filename.php'; command,but haven't worked.

Here is the code:

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 = '[email protected]';                     // 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('[email protected]', 'Email');
        $mail->addAddress($emailTo);     // Add a recipient
        $mail->addReplyTo('[email protected]', '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");
}

So from this php file,I want to send the data of the $url to the mail_template.php

Is this possible to do?

3
  • Please show us the contents of mail_template.php so we can see where/how it is expecting to receive the URL. Commented Sep 19, 2020 at 20:09
  • The mail_template.php is 121 row length. So I think there is no reason to show the full data. Here is a short code from the file: <a data-click-track-id="37" href="<?php echo $url; ?>" style=".........">Reset Password </a> Commented Sep 19, 2020 at 20:13
  • Does this answer your question? PHP Eval that evaluates HTML & PHP Commented Sep 19, 2020 at 20:29

1 Answer 1

0

To send data to another url in php you have to try the following url:

`[email protected]` 

then inside an empty mail_template.php do -

 <?php 
    if (isset($_GET['data'])) {
      $test = $_GET['data'];
//print the data added to the url
      echo $test;
    }
    ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.