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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maros174
Forum Newbie
Posts: 11
Joined: Tue Mar 02, 2010 7:23 am

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

Post 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>.';
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
maros174
Forum Newbie
Posts: 11
Joined: Tue Mar 02, 2010 7:23 am

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

Post 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...
Post Reply