Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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;
      
    ?>
    Last edited by maros174; 03-30-2010 at 01:52 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •