PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Online Offline Manager - Show the online/offline status of a group of users on a webpage


[back]
WB01624_.gif (281 bytes) 12/30/99 "TD Subscribe Mail Script" WB01626_.gif (272 bytes)[next]

Unsubscribing from the mail list

Before I actually start mailing to anyone I need a script that I can link to inside every opt-in mail list email to allow subscribers to easily and automatically unsubscribe from the list. In some places, due to the rampant abuse of spammers, I believe there is legislation which requires such a link, so an ounce of prevention might be worth a pound of cure. My message will look something like this:

------------------------------------------------------------------------------------
This email is not spam. You signed up for this
opt-in email list at php-scripts.com.
You can unsubscribe by following the link at
the bottom of this email message
----------------------------------------------------------------------------------

My actual email contents will go here

-------------------------------------------------------------------------------------
This email is not spam. You signed up for this
opt-in email list at php-scripts.com

If you would like to unsubscribe from the php-scripts.com
script update opt-in email list, just click the link below
http://www.php-scripts.com/php_diary/unsubscribe.php3?id=
if the link above does not show in your email
program just cut and paste it to your browser
-------------------------------------------------------------------------------------

If you actually clicked the unsubscribe.php3 link above you will see that it does nothing. That is because when I create the mail out script I will have PHP embed in each email the proper id number (see the unsubscribe.php3?id= above) to actually activate the script to remove the email from the database and print a message saying removal was successful. This is called using a query string to input data into a script. It is an alternate way to input information into a script instead of using forms and quite useful for situations like the one outlined here.

Now let's look at my code to build an unsubscribe script, you may notice sections of code that are very similar to the mail script we did yesterday:

<?
if($id != "")
{
$emails = file("phpelist.txt");
  for($index = 0; $index < count($emails); $index++)
  {
    if(ereg("$id", $emails[$index]))
    {
      $emails[$index] = "";
      $thefile = fopen("phpelist.txt", "w");
         for($newindex = 0; $newindex <count($emails); $newindex++)
        {
          if($emails[$newindex] != "")
          {
            fputs($thefile, "$emails[$newindex]");
          }
        }
      fclose($thefile);
      print("<HTML><BODY>This email <strong>$id</strong>");
      print(" has successfully been removed from the");
      print(" database.</BODY></HTML>");
    }

  }
}
?>

Notes: If the $id variable exists lets see if it matches any item in the array. A new function being introduced here is called the file function. The file function will dump the contents of a file into an array. In the code above this array is called $emails. Now using the ereg function we can compare the submitted email versus the $emails[] in the database. If a match is found, then we change the array index to be "" (nothing) and then go into a new loop to rebuild the database file. We use the "w" to overwrite the contents of the file, and as long as each item in the array isn't "" (nothing) we put it in the file. Then we print the message that the process was successful. If the email doesn't match then nothing will happen. It would be more useful to insert another small section of code which will tell the person submitting that "this email is not in our database." I will leave this as part of the exercise below.

One structural problem with my mail list script so far, although minor, is what if I want to have a place where people can quickly subscribe or unsubscribe from the list? Well unless they filled out my mail form (12/28/99), they can't currently subscribe. And unless they clicked a link in the email opt-in list or create the correct query string on their own, they can't unsubscribe. Therefore I need to create a script that will do both - subscribe or unsubscribe and then either link it by query string or link it using a form. Below is a form on the page using radio buttons that would function as a vehicle to pass information to such a script:

Join our php-scripts mail list

subscribe                     
unsubscribe 

Example #17: Unsubscribe or subscribe to the php-scripts mail list

Please note: this form will actually subscribe you to our php-scripts opt-in mail list, so if you don't want to subscribe, please be sure to "unsubscribe" your email address when done testing the example.

Now we must write one script to perform the subscribe and unsubscribe functions. Fortunately, I have already shown you the code to do these things; it is a matter of using the code from the mail form (12/28/99) and the code from the unsubscribe script (above) and using one additonal branch of code as a flag: $subscribe. If $subscribe equals "yes" then php will follow the branch of code to validate the email address and add the validated email address to the database, and if the email address is invalid it will print the error message. However, if the $subscribe variable equals "remove" then the unsubscribe branch of code will be executed and the email address will be removed. I also will add a third option -- if neither of the above equal anything, then do nothing, neither subscribe or unsubscribe the email address. You could also create functions to do the above and call the functions.

Exercise #1: Create a single script which subscribes and unsubscribes email addresses from a list (file)

I am going to leave this exercise for you to do on your own because it is a great learning lesson for taking existing code and combining into one useful script, but the code to do this will be in the final version of my mail list script, so that you can see how it was done. By doing this you are demonstrating the ability to read files, write files, alter files, process input from visitors and deal with several useful php functions. However, before the final version of the opt-in mail script can be completed (and thus you can compare your code with mine) we need to create the actual admin function to build and send mail to those on the list. That''s what we're going to do next. You don't need to wait though to play around with the exercise. I'll be here when you get back to proceed with the admin portion of the script :)

In the next diary entry, I will be building a secure browser-based admin area so that I can send mail out. We will explore protecting the admin area using .htacess and using PHP and a hardcoded password.

Please vote on what you think of this diary lesson :)

How useful was this diary entry? Avg Surfer Rating: 118203309692671400000000000000000000000000000000000000000000.00 (423)

[back]WB01624_.gif (281 bytes) 12/30/99 "TD Subscribe Mail Script" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 01/6/00 05:51