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

03-30-2010, 10:10 AM

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:


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

if($emailAddress != NULL) {
$file = fopen('NewsletterREG.txt','a');
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>.';

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.



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.

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



The email address needs to be passed into the querystring e.g.:


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

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

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

echo $msg;