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

251

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?

<?php

error_reporting(1);
session_start();


include 'includes/connect.php';
include 'includes/additional_function.php';
include('classes/phpmailer/phpmailer.php');



if($_POST["Submit"]=="Submit"){

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

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

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

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



 $stmt->execute();

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



 if(!empty($result)){
 $email = $result['email'];
 //echo $email;


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


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

 $stmt->execute(array(
 ':token' => $token,
 ':email' => $email
 ));


 $result1 = $stmt->fetch(PDO::FETCH_ASSOC);
 if(!empty($result)){

 {
// $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
$mail->IsSMTP();

// 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->IsHTML(true);



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


$mail->Body = 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id';
$mail->AltBody = 'Click to Register';


if(!$mail->Send())
{
 echo "Message could not be sent. <p>";
 echo "Mailer Error: " . $mail->ErrorInfo;
 exit;
}

echo "Message has been sent";
}

} 
}

else{


 echo 'You are not Registered';

}

}

?>
232

Answer

Solution:

Firstly, variables don't get parsed in single quotes

$mail->Body = 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id';

wrap it in double quotes

$mail->Body = "http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id";

and will not populate themselves in the email sent.

Which for example:

$token = "abcde";

echo $var = 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id';

echo "<br>";

echo $var = "http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id";

will echo the following:

http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id
http://www.domain.com/register/registration.php?token=abcde&stud_id=stud_id

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

Reference:

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 http://php.net/manual/en/language.variables.superglobals.php 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.

<?php 
error_reporting(E_ALL);
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.


Footnotes:

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

Source

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.