Home > Net >  URL Link not clickable when a Php variable is passed onto it in phpmailer
URL Link not clickable when a Php variable is passed onto it in phpmailer

Time:09-28

Am trying to pass certain php variable as part of a clickable link to be send via PhpMailer. To this effect, I referenced a solution here but still cannot get it to work source

$site = 'http://example.com';
$user_id = 100;

$msg="Please Click links below.<br> <br>
<a  href='$site/link1.php?id=$user_id'>Link 1</a><br><br>
<a  href='$site/link2.php'>Link 2</a>";         

$msg_body = "Here is your $msg";

Here is my issue: When I run the Code, the Email get sent successfully but both link1 and link 2 are not clickable.

here is the code below

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;



// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

$site = 'http://example.com';
$user_id = 100;


$msg="Please Click links below.<br> <br>
<a  href='$site/link1.php?id=$user_id'>Link 1</a><br><br>
<a  href='$site/link2.php'>Link 2</a>";         

$msg_body = "Here is your $msg";



    //PHP Mailer Server settings goes here



    $mail->setFrom('henry@mysite.com', "Henry Good");
      $mail->addAddress($reciever@gmail.com, 'Ann Ball);     // Add a recipient
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'My Email Subject';
    $mail->Body = $msg_body;
    $mail->AltBody = $msg_body; // for Plain text for non-HTML mails
   $sent =  $mail->send();
 

CodePudding user response:

Try to do something like that:

  1. $mail->Body = html_entity_decode($msg_body); or
  2. $mail->set('Body', $msg_body);
  3. In any way try to wrap your message body in <html></html> to get a valid code. Something like my sample below:

$mail->Body = sprintf('<html><body>%s<body><html>', $msg_body);

I hope something from that will help you sort your issue.

CodePudding user response:

An issue i've found so far is the way you concatenated $site and $user_id variable to the content of $msg variable

$site = 'http://example.com';
$user_id = 100;
$msg = "Please Click links below. <br/><br/><a href='" . $site . "/link1.php?id=" . $user_id .
        "'>Link</a><br><br><a href='" . $site . "/link2.php?id=" . $user_id . "'>Link2</a>";

For more on string operations https://www.php.net/manual/en/language.operators.string.php

  • Related