Home arrow PHP arrow Page 3 - Creating a Mailing List Manager with PHP

Adding names to the list - PHP

This tutorial will guide you through building a complete mail list administration solution in PHP, capable of handling multiple lists.

  1. Creating a Mailing List Manager with PHP
  2. Creating the front page
  3. Adding names to the list
  4. Editing the names
  5. Autoresponders
  6. Conclusion
By: Duncan Lamb
Rating: starstarstarstarstar / 52
December 16, 1999

print this article



Here, we’ll automate the process of adding a name to the list:


<html><head><title>Add an email to the list</title></head><body> <br><br> <form method=post action="saveemail.php3"> <table><tr><td valign=top> Which List?: </td><td> <select name="List" size=4> <? $fileloc = "lists.txt"; $groups = file("data/lists.txt"); for ($index=0; $index < count($groups); $index++) { $grouplist = split("\|", chop($groups[$index])); ?> <option value="<? echo $grouplist[1] ?>" <? if ($index==0) {echo "selected";} ?>> <? echo $grouplist[0] ?><br> <? } ?> </select></td></tr></table> Email Address:<input type=text name="Email" size="40"> <br><br> <input type="submit" Value="Add This Email"></FORM> <BR><BR><a href="./">Home</a>. <br><br><a href="picklist.php3">Edit/Delete names</a>. <br><br><a href="data/log.txt">View Send Log</a>. <br> </body></html>
First, a list is chosen in lines 7-21 (this is the same function used on the index page), and then it and the email address is passed to the next script:


<html><head><title>Updating file....</title></head><body> <br><br> <? if (file_exists("data/$List")) { $myfile = file("data/$List"); $fh = fopen("data/$List","w"); for ($index=0; $index < count($myfile); $index++) { if ($Email != chop($myfile[$index])) {fputs($fh,$myfile[$index]);} } fputs($fh,$Email."\n"); fclose($myfile); } else { $myfile = fopen("data/$List","w"); fputs($myfile,$Email."\n"); fclose($myfile); } ?> <br> <? echo $Email ?> written to <? echo $List ?> <br><br> <a href="index.php3">Home</a>. </body></html>
After making sure the file exists, the first thing this script does is read the current list into an array (lines 4-6), then checks for each name to see if it already exists in the list (no one likes being on the same mailing list TWICE!). It does this in lines 8-12 by writing each name back into the file, and just skipping the name if it already exists. Once every name is compared, the new name is written at the end of the file.

You’ll notice that fopen uses "w" here, for writing, while it used "r" on the index page (for reading) and "a" is used on the Send Email page(for appending to an existing file). The format for the command is:

fopen (filename, mode)

With mode being either "r", "w", or "a". Writing an existing file erases the current contents, while appending starts writing at the end of the file. Each of these modes can allow both reading and writing by placing a "+" after the mode. But the initial state (whether the current contents are deleted, and where the initial pointer is) will still occur.

Note: If you are going to open a file for reading and writing simultaneously, nasty things can happen that can destroy data in other files if a problem occurs. Good programming practice dictates you should modify a file by reading it into an array, closing the file connection (which file does automatically), making the modifications, and writing the changed contents back to the file.

Adding lists uses the same technique, Just joining the List and Filenames together with a "|". Here’s the scripts used for to add a list to your collection:


<html><head><title>Mailing List Administration</title></head><body> <br> <b>Both blanks must be filled in!</b><br> <form method=post action="makenewlist.php3"> <b>Name of the list:</b><input type=text name="Listname" size="40"> <br><br> <b>One word description of the list:</b> <input type=text name="Filename" size="40"> </td><td><table><tr><td valign=top> <br><br> <input type="submit" name="Submit" value="Make list"> </form> <br><br><a href="addnames.php3">Add names to an existing list</a>. <br><a href="picklist.php3">Edit/Delete names</a>. <br><br><a href="data/log.txt">View Send Log</a>. <br> </body></html>

<html><head><title>Updating file....</title></head><body> <?$Filename = $Filename.".lst";$myfile = fopen("data/lists.txt","a");fputs($myfile,$Listname."|".$Filename."\n");fclose($myfile);?>Created new list <? echo $Listname ?>.<br><br><br><a href="index.php3">Home</a>.<br><br><a href="addnames.php3">Add names to the list</a>.<br><br><a href="picklist.php3">Edit/Delete names</a>.<br><br><a href="data/log.txt">View Send Log</a>.<br></body></html>

>>> More PHP Articles          >>> More By Duncan Lamb

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: