mysql - How To create a url link and send it to email using php mailer


I created a form where a user types in a code,if the code and email exists in database the code then retrieves the email and a token is generated and updated to the database and has to be sent to that email that is retrieved. I am using php mailer to send an email, can someone help me figure what is wrong with the code below, is the url query correct?



include 'includes/connect.php';
include 'includes/additional_function.php';


 $_SESSION['idcode'] = $post['idcode'];

 $sql = "SELECT * FROM people WHERE idcode = :idcode";

 $stmt = $pdo->prepare($sql);

 $stmt->bindValue(':idcode', $idcode);


 $result = $stmt->fetch(PDO::FETCH_ASSOC);

 $email = $result['email'];
 //echo $email;

 $token = generateToken();
 //echo $token;

 $sql = "UPDATE student SET token = :token WHERE email = :email";
 $stmt = $pdo->prepare($sql);

 ':token' => $token,
 ':email' => $email

 $result1 = $stmt->fetch(PDO::FETCH_ASSOC);

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;

$mail = new PHPMailer();

// set mailer to use SMTP

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server

$mail->SMTPAuth = true; // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: [email protected]
// pass: password
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("[email protected]", "Brad Markle");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML

$mail->Subject = "You Registration Link!";

$mail->Body = '$token&stud_id=stud_id';
$mail->AltBody = 'Click to Register';

 echo "Message could not be sent. <p>";
 echo "Mailer Error: " . $mail->ErrorInfo;

echo "Message has been sent";



 echo 'You are not Registered';






Firstly, variables don't get parsed in single quotes

$mail->Body = '$token&stud_id=stud_id';

wrap it in double quotes

$mail->Body = "$token&stud_id=stud_id";

and will not populate themselves in the email sent.

Which for example:

$token = "abcde";

echo $var = '$token&stud_id=stud_id';

echo "<br>";

echo $var = "$token&stud_id=stud_id";

will echo the following:$token&stud_id=stud_id

As you can see,$token doesn't get its intended value populated, but echos as$token instead ofabcde.


This is assuming your conditional statement and POST arrays are kosher.

Plus this$post['idcode'] needs to read as$_POST['idcode'] as per$idcode=$_POST['idcode']; and error reporting would have helped you here. That's a superglobal and missed the underscore and putting POST in uppercase letters.

If you're unsure:

Add to the top of your file(s) which will help find errors.

ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

or post your HTML form in your question.


Unsure what you want to usestud_id for and how that is supposed to be populated. Only you know that. As per$token&stud_id=stud_id';

Now, if your query is failing, then that's a different story and you would need to find out why that is and is beyond the scope of the question.

People are also looking for solutions to the problem: Call count result from stored procedure MySQL in PHP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.