The structure of the application will be straightforward. All the scripts will be in the main directory, and all data files in a data/ directory. Each list will have its addresses stored in a separate file in that directory. There will also be a log file, saved as a simple text file.
The lists we are going to use (only one at the moment) are stored in the lists.txt file in the data directory. The format for the file has one list per line, with the "familiar" list name and the filename for the list separated by a pipe ("|"). This file will be used to build the select box used to specify which list to perform a particular action on. By grabbing the filename ($Filename) from a form, it is easy to grab the addresses out of the specified list.
I’m a big believer in putting the most often used features up front for ease of use. Once the application is up and running, the most often-used feature would be sending an email to the list. With that in mind, we’ll build a "Send to" form on the index page, along with links to all the administrative functions:
index.php3
<html><head><title>Mailing List Administration</title></head><body>
<br>
<center><H1>Mailing List Administration</H1></center>
Send an email to a mailing list:
<form method=post action="sendemail.php3">
<table><tr><td>
<b>From Address:</b>
<input type=text name="From" size="40" value="">
<br>
<b>Subject:</b><input type=text name="Subject" size="40">
</td><td><table cellspacing=15><tr><td valign=top>
<b>List:</b>
</td><td>
<select name="List" size=4>
<?
$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>
<td valign=top><b><a href="newlist.php3">Make a new list.</a></b>
<br><a href="addnames.php3">Add names to a list</a>.
<br><a href="picklist.php3">Edit/Delete names</a>.
<br><a href="data/log.txt">View Send Log</a>.
<br><a href="autoresponder.php3">View/Edit Autoresponder</a>.
</td></tr></table>
</td></tr></table>
Type or paste your message below:
<br><textarea cols=50 rows=10 name="Body"></textarea>
<br><br>
<input type="submit" name="Submit" value="Send Mail">
</form>
<br>
</body></html>
You’ll notice the entire script is normal HTML, except for
lines 14-26. These lines read in the lists.txt file, spilt each line on the "|", and format them into the select list. (The file function is explained in more detail below.) This produces a tidy list, which will display the common name of the list to the user, and store the filename for the list as the value of the picked option.
Note: To make things even easier, set a default "value" for the "From Address" in the form. By doing so, that blank will automatically be filled in for you, but still allow you to change it if needed.
By grabbing the filename ($Filename) from this form on the index page, it is easy to grab the addresses out of the specified list.
When submitted, an entire list is mailed the message. The data goes directly to this script:
sendemail.php3
<html><head><title>Updating file....</title></head><body>
<?
$addresses = file("data/$List");
for ($index=0; $index < count($addresses); $index++)
{
mail("$addresses[$index]","$Subject",
"$Body","From: $From\nReply-To: $From");
}
$myfile = fopen("data/log.txt","a");
fputs($myfile,$Subject."\t".date("dS of F Y h:i:s A")."\t".$List."\n");
fclose($myfile);
?>
Your message was sent!
<br><br>
<a href="index.php3">Home</a>.
</body></html>
This script makes use of some very useful functions. First is the mail function (line 7), one of the most high-impact, yet easy to use functions that exist in PHP. By default, it accesses sendmail on a UNIX system, but this can be changed to another server by editing the php.ini file. The format for using it is as follows:
mail(string to, string subject, string message, string [additional_headers]);
For additional headers, you can include everything observed by looking at the headers of your mail messages, including X-Mailer, Reply-To, CC:, Bcc, Mime-Version, you can even make up your own headers if you are so inclined.
To retrieve email addresses (lines 3-5), the file function is used. This function reads the entire file into an array, with each line in the file represented by an element in the array. Since the names are stored in the file one to a line, each element in the array will contain an email address. Once this is done, looping through the addresses, and calling mail for each one will send your message out individually to each list member.
Note: An important caveat to using the file function is that the newline is also stored in each array element. This makes it necessary to chop() or trim() each element in many situations.
The final step in the script is to make a log entry so we can track the messages we send (lines 11-13). Here a log is kept in its simplest form, just a text file. The "\t" characters used translate to tabs, much like "\n" translates to a newline character.
Another interesting function is used here – the date function. It has some rather involved formatting rules, which are adequately explained in the PHP documentation. Be sure to read it, and try to use it in sensible places, as it does allow quite a few options.