Page 1 of 1

Please help --- PHP unsubscribe to a newsletter, no database

Posted: Tue Mar 30, 2010 3:04 am
by maros174
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:

Code: Select all

<?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>.';
?>

Re: Please help --- PHP unsubscribe to a newsletter, no data

Posted: Tue Mar 30, 2010 11:03 am
by AbraCadaver
Several ways to do it. Here's one:

Code: Select all

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

if($emailAddress != NULL) {
    $file = file_get_contents('NewsletterREG.txt');
    $file = str_replace($emailAddress.PHP_EOL, '', $file);
    file_put_contents('NewsletterREG.txt', $file);
}

Re: Please help --- PHP unsubscribe to a newsletter, no data

Posted: Wed Mar 31, 2010 2:50 am
by maros174
I'm sorry, it seems my programming is way worse than I thought - I don't understand how to integrate your code... Here's what I do, step by step, i would really appreciate it if somebody showed me what to do:

1. I first make an HTML version of the newsletter, you can view it here:
Zagrebačka burza - komentar stanja i kretanja cijena dionica

(In the bottom, above the gray shaded area, is the sentence stating that if you want to unsubscribe contact us at -> mailto: newsletter@investa.hr - instead of that the regulator insists I have to put a link that is going to automatically unsubscribe the recipient of that email - that's what I need)

2. I open it in a browser, ctrl-a (select all)

3. Copy to word, File->Send to-> mail recipient (via outlook)

That's all, there is no php code anywhere so I don't know where to put the AbraCadaver's code, or how to modify the link to activate it...