I am using a php script for sending email alerts to a list of recipients that I have in MySQL. I want to automate the unsubscribe process for recipients because right now I have a static page where they have to manually enter their info. Since anyways I am calling out the variables in my script for each individual recipient to send emails, I can perhaps also customize the unsubscribe url for each that can have info about their email and keyword that they had initial signed up for. Do you guys think it is possible to auto populate my form fields using this method? Or would you recommend any other method? Here's a brief code

$rr=$row["keyword"];
		$tt=$row["emailid"];
        $to= $tt;
	$subject = 'New updates for '.$rr;
	$message='<html><body><b>Alerts for '.$rr.'</b><br><br>';
	$message.='<b>'.$total.' updates found</b><br>';
	$i=1;
	while ($row= mysql_fetch_array($query))
 	 {
$message.='<br><span style="font-size:13px"><b>';
$message.=$i.') '.$row["title"].'</b></span><br>';
 		$message.=' '.$row["link"].'<br>';
 		$i=$i+1;
 	 }
 	$message.='<br><br><br>Thanks for setting up alerts ';
	$message.='To unsubscribe from daily alerts, please click the link below,<br>';
	$message.='<a href="http://www.domain.com/alert_unsubcribe.php">Unsubscribe</a>';
	$message.='</body></html>'; 
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

The most basic method would be to attach their email address to the unsubscribe url and then use $_GET at the unsubscribe page to update your database and then just display a page to them to say the have unsubscribe. Briefly:

<a href="http://www.domain.com/alert_unsubcribe.php?email={$tt}">Unsubscribe</a>

Then at the unsubscribe page:

if (isset($_GET['email']) {
mysql_query("UPDATE recipients SET subscribed=0 WHERE email={$_GET['email']} LIMIT 1");

Or delete their details if that is how you have things set up.

Member Avatar for diafol

I agree with simplypixie. I'd go further and include a confirmation hash to go with the email address, e.g.

$conf = hash("sha256", 'saltysaltandmoresalt' . $tt . 'evenmoresalt');

$message.="<a href=\"http://www.domain.com/alert_unsubcribe.php?email={$tt}&conf={$conf}\">Unsubscribe</a>";

That should avoid malicious 3rd party unsubscribers.

Thanks Ardav and simplypixie, I will try it out. I am using both email and keyword combination to unsubscribe. How can I integrate that into the link? Can you guys please help me with the php syntax on unsubscribe page. I appreciate your help..

<a href="http://www.domain.com/alert_unsubcribe.php?email={$tt}&keyword={$rr}">Unsubscribe</a>

just like ardav said you can just encrypt your keyword in your case $rr

$keyword = hash("sha256", 'saltysaltandmoresalt' . $rr . 'evenmoresalt');

echo "<a href=\"http://www.domain.com/alert_unsubcribe.php?email=".$tt."&keyword=".$keyword."\">Unsubscribe</a>";

I made it work with a simple unsubscribe link.. but I am having a weird problem now. The keyword that I want pull up on the webpage is only showing the first word and skips all following words. Email is coming up fine. Why would this happen?

Member Avatar for diafol

Can you give an example of what SHOULD display and what's ACTUALLY showing?

You may have to go 'Use Advanced Editor' and uncheck 'Automatically parse links in text' under Miscellaneous Options to display the urls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.