cancel subscription through mail

Hey back again :slight_smile:
I’m trying to improve my news letter feature which allows people to register their emails, and as they do their emails get added to the database.
Whenever I send out a news letter using the mail() function, I want the message to include a link that will remove them from the db and therefore canceling their subscription.
The problem I’m having is that I’m not sure how to get the desired email…

[php]
include ‘core/init.php’;
include ‘includes/overall/header.php’;
$errors = array();

if(empty($_POST) === false) {
	
	

	if(empty($_POST['message']) === true || empty($_POST['subject'])=== true) {
		$errors[] = 'Both fields are required.';
	} else {
		
                    $remove_email = ???
		$query = "SELECT email FROM rss";
		$emails = mysql_query($query) or die(mysql_error());
		$subject = $_POST['subject'];
		$message = $_POST['message'] . " \n \n To cancel your subscription, please follow this link. \n http://******.php?" .$remove_email;
				

		while($rows = mysql_fetch_assoc($emails)) {
			mail($rows['email'], $subject, $message, 'From: ****** - News Letter');
		}
		header('Location: new-rss.php?sent');
		exit();
	}
}

?>
[/php]

Any help is appreciated

you need to narrow down your query by adding a where statement.

your current query will fetch ALL emails which im sure you don’t want that.

[php]

<?php include 'core/init.php'; include 'includes/overall/header.php'; $errors = array(); if(empty($_POST) === false) { if(empty($_POST['message']) === true || empty($_POST['subject'])=== true) { $errors[] = 'Both fields are required.'; } else { //use something unique on your WHERE statement $query = "SELECT email FROM rss WHERE `column1` = 'something'"; $emails = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($emails) > 0) { $subject = $_POST['subject']; while($rows = mysql_fetch_assoc($emails)) { $message = $_POST['message'] . " \n \n To cancel your subscription, please follow this link. \n http://index.php?email=".$rows['email']; mail($rows['email'], $subject, $message, 'From: ****** - News Letter'); } } header('Location: new-rss.php?sent'); exit(); } } ?>

[/php]
it should work i didnt test it so let me know if it works.

you need to fix the query and change the index.php in side the while loop to the correct file name.

that ‘something’ is what I’m struggling with… I only got 2 fields in my table, Id and email…

the purpose of thi, is for newsletter with a unsucubscribe option ?

yes

this is the script that will send email or the script that will remove an email from Database?

well this is the script that sends the email, and then inside the message is a link to the page that has the deletion script.

is there any function that returns a string of the current url? if so I got a solution I think

ok i got it
[php]

<?php include 'core/init.php'; include 'includes/overall/header.php'; $errors = array(); if(empty($_POST) === false) { if(empty($_POST['message']) === true || empty($_POST['subject'])=== true) { $errors[] = 'Both fields are required.'; } else { //use something unique on your WHERE statement $query = "SELECT email FROM rss"; $emails = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($emails) > 0) { $subject = $_POST['subject']; while($rows = mysql_fetch_assoc($emails)) { $message = $_POST['message'] . " \n \n To cancel your subscription, please follow this link. \n http://index.php?email=".$rows['email']; mail($rows['email'], $subject, $message, 'From: ****** - News Letter'); } } header('Location: new-rss.php?sent'); exit(); } } ?>

[/php]

this script will send an email containing a link like this
http://[email protected]

now on the script that its gonna do the delete process you get value of super global Get Variable email

and then run a query where email =$_GET[‘email’]

let me know how is everything works for you

ok atm i’m trying to get the last part of the url (which should be the email )and store it in a variable.

For some reason it’s not displaying the ‘@’ symbol instead it replaced it with ‘%40’, any ideas?

yes that right, An URL can not contains some symbols therefore they are convert to urlencoded
to decode it back you can call this fucntion urldecode

[php]
urldecode($email);
[/php]

however i have a better Idea.
on your newletter link to the delete page and have them input their email on a textbox

well the only problem with that is that then anyone can enter a random email… which maybe is unlikely to happen… but still ;D

I read up on that %40 part, apparently that’s just the URL, the server still reads it as @.

the problem right now is…
I get the desired email, but at the moment it’s displayed as /cancel-rss.php?email=‘[email protected]

Is there any way I can save just the part with the ‘[email protected]’ in a variable? excluding the
“/cancel-rss.php?email=”

of course buddy when i said to call the function urldecode

that is what i meant

[php]
$email = urldecode($_GET[‘email’]);
[/php]

now $email will contains the @.

about my idea yes many people will input wrong email but you are running a query and the query will succeed or fail if it failed echo back “the email was not found on the database”

use any method that you think will working bettter

this is the deletion script atm;
[php]
$url = $_SERVER[‘REQUEST_URI’];
parse_url($url);
$email = $url[1];
$email = urldecode($email);
print_r($email);
mysql_query(“DELETE FROM rss WHERE email = ‘$email’”);
[/php]

as I print out the $url I get correct email, but it’s still displaying %40 instead of @

and when I print out $email I get the letter C

strange? xD

ahh fixed it!

[php]
//get url from server
$url = $_SERVER[‘REQUEST_URI’];

// decode, get @ instead of %40 and store it in $email
$email = urldecode($url); 

// parse the $email so its values are stored in an array
$parsed_url = parse_url($email);

// get the value from the array
$true_email = $parsed_url[query];

// remove email from database
mysql_query("DELETE FROM `rss` WHERE `email` = '$true_email'");

[/php]

Solved! thread can be closed/removed now :slight_smile: thanks wilson for your help

[php]

<?php if (isset($_GET['email'])) { $email = urldecode($_GET['email']); print_r($email); mysql_query("DELETE FROM `rss` WHERE `email` = '$email'"); } ?>

[/php]

now tell me what you get when you print $email

It outputs nothing… neither does it remove the email from the db.

If the URL do not contains the ?email it will print nothing

for instance you have this URL

http://example.com/delete.php?email=wilson382%40hotmail.com

when you run this script it will output [email protected]
[php]

<?php if (isset($_GET['email'])) { $email = urldecode($_GET['email']); print_r($email); mysql_query("DELETE FROM `rss` WHERE `email` = '$email'"); } ?>

[/php]

I saw that already solved this anyway
Karma me if i helped

good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service