简体   繁体   中英

Insert PHP variable to href link in HTML

I am sending a confirmation mail to user to confirm his account. The confirmation mail is styled using HTML and has an href element which points the users to a PHP file where I do the confirmation process. This href link also needs to have a PHP randomstring attached, the same randomstring is saved in Database and is also sent to user so that the cross-checking can be done in PHP once the user clicks on it.

  <td align="center" style="margin:0;text-align:center">
    <a href="http://aliencreative.wehubs.com/new_alien/email.php?code=<?php echo $randomString; ?>" 
    style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;
    border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;
    letter-spacing:1.2px" target="_blank">Confirm Alien account now!</a></td>

The PHP code includes the above HTML as follows.

<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;

echo $random;
$to = 'sample@gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.php");

// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Additional headers
$headers .= 'From: Alien creative control<alien@alien.com>' . "\r\n";


// Send email
if(mail($to,$subject,$htmlContent,$headers)):
    $successMsg = 'Email has sent successfully.';
else:
    $errorMsg = 'Some problem occurred, please try again.';
endif;

?> 

However, the PHP variable is't getting available in the link.

Please try this. Remove file_get_contents and use include with output buffer function. I think this will insert your variable to your template. Please check the below updated code. Anyway your method is not a secure. Please create more complicated random code

        <?php
    $randomString=time();
    //$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;

    echo $random;
    $to = 'sample@gmail.com';
    $subject = "Confirmation mail";
    // Get HTML contents from file

    ob_start();
    include "email_template.php";
    $htmlContent=ob_get_contents();
    ob_end_clean();

    // Set content-type for sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // Additional headers
    $headers .= 'From: Alien creative control<alien@alien.com>' . "\r\n";


    // Send email
    if(mail($to,$subject,$htmlContent,$headers)):
        $successMsg = 'Email has sent successfully.';
    else:
        $errorMsg = 'Some problem occurred, please try again.';
    endif;

    ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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