...

View Full Version : Please help --- Unsubscribe to a newsletter - PHP, no database



maros174
03-30-2010, 10:10 AM
Hi,

I have a super simple (working) Newsletter subscribe form on my site. It uses php to store email addresses in a flat txt file on a server.

Can anybody show me how to write a php code like this one below, but which would be used to UNSUBSCRIBE. I would place a "Click here to unsubscribe" link to it in the bottom of my newsletter - User clicks on that link from his email, and his email address is stored in an unsubscribe.txt file on my server.


Here's the code I use for subscription to a newsletter:

<?php

$emailAddress = (isset($_POST['email'])) ? strip_tags($_POST['email']) : NULL;

if($emailAddress != NULL) {
$file = fopen('NewsletterREG.txt','a');
fwrite($file,$emailAddress.PHP_EOL);
fclose($file);
}
header("Refresh: 3; url=\"http://www.MySite.com\"");
echo ('<br>Thank You!');
echo '<br><br>If you are not automatically redirected, click here: <a href="http://www.MySite.com">www.MySite.com</a>.';
?>

mlseim
03-30-2010, 01:33 PM
So I assume you want it removed from one file and added to the unsubscribe file?

I would take a slightly different approach. Instead of using another file, you could
just append a new field onto your flat-file database.

Like:

john@aol.com|1
bill@comcast.net|0
janedoe@qwest.com|1

Where 1 = subscribed
and 0 = unsubscribed

Fields separated by pipes |

You keep their email even if they unsubscribe, but you change the enable from 1 to 0.

When you read your flat-file database, you only pick out the enabled names.
This means you only need to work with one file.

Of course MySQL would even be easier, but you asked about the file you now have.

maros174
03-30-2010, 02:22 PM
Thanks, your solution is similar to what I had in mind because I dont want to remove the addresses of all subscribers from NewsletterREG.txt.... I only want to add those who unsubscribed to another file: unsubscribed.txt....I will manually edit my recipient list using those two files

I can do this with the code below, but I don't know how make the link in the bottom of my newsletter which passes user's email address to a variable $emailAddress



<?php

/*

Unsubscribe
The email address needs to be passed into the querystring e.g.:
http://mysite.com/unsubscribe.php?email=someone@somewhere.com

*/

$emailAddress = (isset($_GET['email'])) ? strip_tags($_GET['email']) : NULL;

if($emailAddress != NULL) {
$file = fopen('UNSUBSCRIBED.txt','a');
fwrite($file,$emailAddress.PHP_EOL);
fclose($file);

$msg = "Your request for unsubscription has been accepted, you will receive no further contact from us.";
}
else
{
$msg = "Sorry, something went wrong we could not unsubscribe your email address, please contact us.";
}

echo $msg;

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum