Reputation: 93
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 .= '&';
//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('&',$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.
Upvotes: 1
Views: 819
Reputation: 1120
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?
Upvotes: 1
Reputation: 2229
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.
Upvotes: 2