1

I have URL which is sent to the given email. However, I would like user clicking that URL to be redirected to another URL.

I have tried the below code for sending mail,but I don't know how to create a URL which will redirect to another page.

<?php

$send_link=getLink("http://www.google.com",array("referral"=>"123","mobilenumber"=>"1234"));
echo $send_link;


function getLink($url,$params=array(),$use_existing_arguments=false)
{

    if($use_existing_arguments) $params = $params + $_GET;

    if(!$params) return $url;
    $link = $url;

    if(strpos($link,'?') === false) $link .= '?';
    //If there is no '?' add one at the end
    elseif(!preg_match('/(\?|\&(amp;)?)$/',$link)) $link .= '&amp;';
    //If there is no '&' at the END, add one.
    $params_arr = array();
    foreach($params as $key=>$value)
    {

        if(gettype($value) == 'array')
        {
            //Handle array data properly
            foreach($value as $val)
            {
                $params_arr[] = $key . '[]=' . urlencode($val);
            }

        }
        else
        {
            $params_arr[] = $key . '=' . urlencode($value);
        }

    }

    $link .= implode('&amp;',$params_arr);
    return $link;
}

$user_id='1';
$get_referred_user=mysqli_query($con,"select * from Phone_Book_Email where contact_id='$user_id'");

if (mysqli_num_rows($get_referred_user) > 0)
{
    while($row = mysqli_fetch_assoc($get_referred_user))
    {
        $email_id=$row['email_id'];
        echo $email_id;
        $to      = $email_id;
        $subject = 'test';
        $message = 'URL- '.$send_link;
        $headers = 'From: Hubster@hubster.com' . "\r\n" .    'Reply-To: Hubster@hubster.com' . "\r\n" .    'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $message, $headers);
        //header("Location:http://www.facebook.com");
        //header("refresh: $time_in_seconds; url=http://www.facebook.com");
    }

}


?>

Please suggest me guys what I have to do here for redirecting user clicking one url to another url.

Abdel5
  • 1,112
  • 3
  • 16
  • 39
Sakura
  • 93
  • 1
  • 1
  • 10

2 Answers2

2

It's honestly very simple what you need.

<a href="http://facebook.com">google.com</a>

So in this case, it will show you google.com as the href link, but when you click on it, you'd go to facebook.

Akshay
  • 2,244
  • 3
  • 15
  • 34
1

To have one link pointing to another adress you will have to use in your email message HTML tags.

This means that in the header of your email you should use additionally:

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Than in your message you can simply use HTML tags:

<HTML><BODY><A HREF="$link_a">$link_b</A></BODY></HTML>

where $link_a - is uri of redirect, and $link_b is link to be displayed.

Update 1 relating to the code in the comment:

As to the code provided by you in the comment there is one mistake in line

$message = 'URL- '."<A HREF=\"$send_link\">$link_b</A>"; 

it should look like this:

$message = "URL- "."<A HREF=\"$send_link\">$link_b</A>"; 

or best:

$message = "URL- "."<A HREF=\"".$send_link."\">".$link_b."</A>"; 

Update 2 full code:

You were also missing "\r\n" after X-Mailer: PHP/' . phpversion() which could resulted in email not being formatted. Try something like this:

     $to = $email_id; 
    $subject = 'test';
    $send_link = 'http://google.com';
    $link_b = 'http://facebook.com';
 $message = "URL- "."<A HREF=\"".$send_link."\">".$link_b."</A>"; 
 $headers = 'From: Hubster@hubster.com' . "\r\n" .
 'Reply-To: Hubster@hubster.com' . "\r\n" .
 'X-Mailer: PHP/' . phpversion()."\r\n";
 $headers .= "MIME-Version: 1.0\r\n";
 $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 mail($to, $subject, $message, $headers);

You may also want to refer to information provided here: Send HTML in email via PHP?

Community
  • 1
  • 1
Abdel5
  • 1,112
  • 3
  • 16
  • 39
  • Hey @Abdel5,I have tried your code , it should be like this only no.. ? $to = $email_id; $subject = 'test'; $message = 'URL- '."{$link_b}"; ; $headers = 'From: Hubster@hubster.com' . "\r\n" . 'Reply-To: Hubster@hubster.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; mail($to, $subject, $message, $headers); – Sakura Aug 05 '15 at 06:19
  • Please remove { } tags as shown in the updated reply. – Abdel5 Aug 05 '15 at 06:26
  • hey @Abdel5 if I am using $headers which you suggest me ,message not sending on email but if i am using without that $headers it's working,message sending. – Sakura Aug 05 '15 at 06:33
  • I have tried your updated code,message is sending and that link looks like this ..http://www.facebook.com – Sakura Aug 05 '15 at 06:34
  • This is probably because after 'X-Mailer: PHP/' . phpversion()' you should have "\r\n". – Abdel5 Aug 05 '15 at 06:39
  • Thank you so much :-) @Abdel5 ,its working perfectly fine for me , i have tried your updated answer and its working. – Sakura Aug 05 '15 at 07:08
  • hey @Abdel5 do u now how can i get the values from that link ,if that link will be click by someone.??? – Sakura Aug 05 '15 at 10:27
  • Neha, I don't quite understand your question. Possibly you could write a bit more about your problem in another question (just to not make side discussions here) and link it here so I will be able to see if I can help. – Abdel5 Aug 05 '15 at 11:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85195/discussion-between-abdel5-and-neha). – Abdel5 Aug 05 '15 at 11:21