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: [email protected]' . "\r\n" .    'Reply-To: [email protected]' . "\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.

7
  • 1
    Do you mean that you want one link to be shown in the message and another address to be opened in browser when link is clicked? Or are you looking to redirect HTTP requests from one address to another?
    – Abdel5
    Aug 5, 2015 at 5:59
  • hey @Abdael5 thanks for reply..you are getting correct,I want to one click to be shown in the message and that link should be redirect on another link...
    – Sakura
    Aug 5, 2015 at 6:02
  • Are both the links on your server ?
    – Akshay
    Aug 5, 2015 at 6:04
  • No @Akshay,I am just trying with another link , like 1st link will be www.google.com and 2nd link will be www.facebook.com.
    – Sakura
    Aug 5, 2015 at 6:07
  • 1
    @Neha <a href="link1.php">Link2.php</a> Something like this?
    – Akshay
    Aug 5, 2015 at 6:12

2 Answers 2

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.

2
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: [email protected]' . "\r\n" .
 'Reply-To: [email protected]' . "\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?

9
  • Hey @Abdel5,I have tried your code , it should be like this only no.. ? $to = $email_id; $subject = 'test'; $message = 'URL- '."<A HREF="{$send_link}">{$link_b}</A>"; ; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\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 5, 2015 at 6:19
  • Please remove { } tags as shown in the updated reply.
    – Abdel5
    Aug 5, 2015 at 6: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 5, 2015 at 6:33
  • I have tried your updated code,message is sending and that link looks like this ..<A HREF="google.com?referral=123&amp;mobilenumber=123">http:/…>
    – Sakura
    Aug 5, 2015 at 6:34
  • This is probably because after 'X-Mailer: PHP/' . phpversion()' you should have "\r\n".
    – Abdel5
    Aug 5, 2015 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.