Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:52:51+00:00 2026-05-30T09:52:51+00:00

I run a small website and my users requested that I set up a

  • 0

I run a small website and my users requested that I set up a mailing list. I found a simple free script that adds e-mail addresses to a protected text file, email.txt, in CSV format:

email1@yahoo.com,email2@yahoo.com,blah,blah,blah

The script works flawlessly. However, it is a nuisance to go through the file to manually remove e-mail addresses when users cancel their list subscriptions. I need to create a simple script that removes e-mail addresses.

All I want is a simple PHP script that displays a text box so users can enter their e-mail addresses and click a “Cancel Newsletter” button. The script should search the text file, find the given e-mail address and remove it and its trailing comma.

For example, say the contents of email.txt are

john@yahoo.com,peter@yahoo.com,steve@yahoo.com

If I type “peter@yahoo.com” into the text box displayed by my desired script, I want the file to look like this:

john@yahoo.com,steve@yahoo.com

UPDATE: I tried this code:

<?php
        function showForm() {
            echo '
            <form method="post" action="">
            Email Address: <input type="text" name="email"> <br />
            <input type="submit" value="Cancel Newsletter" name="submit">
            </form>
            ';
        }

        $_POST['email']

        $to_delete = 'email';
        $file = array_flip(explode(",",file_get_contents("email.txt")));
        unset($file[$to_delete]);
        file_put_contents("email.txt",implode(",",array_flip($file));

        if(!$file_put_contents) {
            die('Error occured');
        } else {
            echo 'Your subscription has been cancelled. You will not receive any further emails from us.';
        }
    }
} else {
    showForm();
}
?>

This code doesn’t even show the form.

UPDATE 2:

Another attempt at writing this script:

<?php
    $email = $_POST["email"];
    $text = file_get_contents("email.txt");
    $oldWord = "$email";
    $newWord = "";
    $text = str_replace($oldWord , $newWord , $text);
    $fp = fopen('email.txt', 'w');
    fwrite($fp, $text);
    fclose($file);
?>

This works as far as removing the e-mail address goes, but there is no announcement (echo). I would like for it to say either “that e-mail isn’t subscribed” or “you have been removed,” based on whether the script sucessfully finds the $email in the list and deletes it.

UPDATE 3 Dec. 31, 2011:

I tried the advanced code and just got a blank page, so I went back to my version. Here is the code I have now:

<html>
    <body>
        <form method="post" action="">
            <p>Email Address: <input type="text" name="email"></p>
            <input type="submit" value="Cancel Newsletter" name="submit">
        </form>

        <?php
            $email = $_POST["email"];
            $basetext = file_get_contents("email.txt");
            $oldWord = "$email";
            $newWord = "";
            $text = str_replace($oldWord , $newWord , $basetext);
            $str1 = $basetext;
            // echo strlen($str1);
            $str2 = $text;
            // echo strlen($str2);
            $fp = fopen('email.txt', 'w');
            fwrite($fp, $text);
            if ($str1 > $str2) { // This part handles the success/fail message
                echo ("Success!");
            } else {
                echo ("Fail!");
            }
            fclose($file);
        ?>
    </body>
</html>

This works perfectly. However, it displays the “fail” message when the page is loaded, not when triggered to load, after the submit button is pressed.

I would like to keep the original code if possible, just rearranged so that it only shows “Success!” or “Fail!” once it has executed the code.

I would like the echo messages to be the last script executed on the page.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:52:53+00:00Added an answer on May 30, 2026 at 9:52 am

    This answer was originally appended to the question body by the OP.

    First I moved the form to /cancel.html and used <form action="/cancel_submit.html">.

    (Where I have written .html, it is just to demonstrate, as my server is configured to use no page extentions and also so that PHP is parsed on .html pages.)

    Then I put the PHP code into the page /cancel_submit.html and moved

    if ($str1 > $str2) {
        echo ("You Have Successfully Unsubscribed From Our Newsletter....<br>You Will Not Receive Any More Emails From Us.");
    } else {
        echo ("The Email Address You Specified Is Not In Our Mailing List.");
    }
    

    to another set of PHP brackets.

    This meant that the e-mail adddress was sent via POST to the other page, which then performed the actual removal of the e-mail address from the list and then checked to see if an address been removed to provide the comfirmation message.

    I also added two commas to $oldword = "$email"; to make it $oldword = ",$email,"; so that it only finds the text entered into the e-mail box if it has a comma on either side. This addresses the case where someone submits half of an e-mail address.

    I also changed $newWord = ""; to $newWord = ","; so that if the script removes an e-mail address with commas at each side, the two e-mail addresses that were next to it will not be separated by a comma.

    Here is the code I have for both pages now:

    1. cancel.html

      <p>To cancel our Newsletter please enter your email address below....</p>
      <p>
      <form method="post" action="/cancel_submit.html">
      <p>Email Address: <input type="text" name="email"></p>
      <input type="submit" value="Cancel Newsletter" name="submit">
      </form>
      
    2. cancel_submit.html

      <?php
          $email = $_POST["email"];
          $basetext = file_get_contents("email.txt");
          $oldWord = ",$email,";
          $newWord = ",";
          $text = str_replace($oldWord , $newWord , $basetext);
          $str1 = $basetext;
          // echo strlen($str1);
          $str2 = $text;
          // echo strlen($str2);
          $fp = fopen('email.txt', 'w');
          fwrite($fp, $text);
          fclose($file);
      ?>
      <?php
          if ($str1 > $str2) {
              echo ("You Have Successfully Unsubscribed From Our Newsletter....<br>You Will Not Receive Any More Emails From Us.");
          } else {
              echo ("The Email Address You Specified Is Not In Our Mailing List.");
          }
      ?>
      <p>
          <p>Please wait to be re-directed or <a href="http://the-flying-shuttle.com/"><u>CLICK HERE.</u></a>
          </p>
      

    EDIT:

    I made a few improvements. I added:

    $email = strtolower($email);
    

    to both the e-mail add script and the e-mail remove script. This converted all characters entered into either form to lowercase; previously, it wouldnt remove e-mails typed in a different case than the big list had.

    This messed up the confirmation message command, so I changed it to

    if (str_replace($oldWord , $newWord , $basetext)) {
        echo ("You Have Successfully Unsubscribed From Our Newsletter....<br>You Will Not Receive Any More Emails From Us.");
    } else {
        echo ("The Email Address You Specified Is Not In Our Mailing List.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Simple PHP Mailer Script i run a small website and we have
I run a small php/mysql website for a camera club where users can upload
Well I run a small video website and on the actual video page there
Every time I try to run a small application that uses a Derby DB
Background: I'm building a small application that will be run daily, pulling data from
I've got a very small standalone vb.net app that gets run automatically. Every now
I'm building a small website with JQTouch and the first problem I've run into
So anyways, I'm working on a small PHP website/script, and as one of the
I'm trying to make a script that scrapes a website to retrieve the latest
I'm trying to run the following code on a website to set up the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.