672 Chapter 28 Building a Mailing List Manager $message->setTXTBody($text); $message->setHTMLBody($html); We then load the image details from the database and loop through them, adding each image to the piece of mail we want to send: // load each image from disk $imgfilename = "archive/$listid/$mailid/".mysql_result($result, $i, 0); $imgtype = mysql_result($result, $i, 1); // add each image to the object $message->addHTMLImage($imgfilename, $imgtype, $imgfilename, true); The parameters we pass to addHTMLImage() are the name of the image file (or we could also pass the image data), the MIME type of the image, the filename again, and true to signify that the first parameter is a filename rather than file data. (If we wanted to pass raw image data we would pass the data, the MIME type, an empty parameter, and false.) These parameters are a little cumbersome. At this stage we need to create the message body.We need to do this before we can set up the message headers.We create the body as follows: // create message body $body = $message->get(); We can then create the message headers with a call to Mail_mime’s headers() function: // create message headers $from = '"'.get_real_name($admin_user).'" <'.$admin_user.'>'; $hdrarray = array( 'From' => $from, 'Subject' => $subject); $hdrs = $message->headers($hdrarray); Finally, having set up the message, we can send it. In order to do this we need to instantiate the PEAR Mail class and pass to it the message we have created.We begin by instantiating the class, as follows: // create the actual sending object $sender =& Mail::factory('mail'); (The parameter 'mail' here just tells the Mail class to use PHP’s mail() function to send messages.We could also use 'sendmail' or 'smtp' as the value for this parameter for the obvious results.) Next we send the mail to each of our subscribers. We do this by retrieving and looping through each of the users subscribed to this list, and using either the Mail send() or regular mail() depending on the user’s MIME type preference: 34 525x ch28 1/24/03 2:55 PM Page 672 673 Next if($subscriber[2]=='H') { //send HTML version to people who want it $sender->send($subscriber[1], $hdrs, $body); } else { //send text version to people who don't want HTML mail mail($subscriber[1], $subject, $text, 'From: "'.get_real_name($admin_user).'" <'.$admin_user.">"); } The first parameter of $sender->send() should be the user’s email address, the second the headers, and the third the message body. That’s it! We have now completed building the mailing list application. Extending the Project As usual with these projects, there are many ways you could extend the functionality.You might like to n Confirm membership with subscribers so that people can’t be subscribed without their permission.This is typically done by sending email to their accounts and deleting those who do not reply.This approach will also clean out any incorrect email addresses from the database. n Give the administrator powers to approve or reject users who want to subscribe to their lists. n Add open list functionality that allows any member to send email to the list. n Let only registered members see the archive for a particular mailing list. n Allow users to search for lists that match specific criteria. For example, users might be interested in golf newsletters. Once the number of newsletters grows past a par- ticular size, a search would be useful to find specific ones. n If you expected to have a large mailing list a PHP application would not be the ideal way to send out the messages. A purpose built mailing list manager like exmlm would be much more efficient that calling mail() many times in PHP. Of course you could still build the front end in PHP, but have exmlm handle the grunt work. Next In the next chapter, we will implement a Web forum application that will enable users to have online discussions structured by topic and conversational threads. 34 525x ch28 1/24/03 2:55 PM Page 673 34 525x ch28 1/24/03 2:55 PM Page 674 29 Building Web Forums ONE GOOD WAY TO GET USERS TO return to your site is to offer Web forums.These can be used for purposes as varied as philosophical discussion groups and product tech- nical support. In this chapter, we implement a Web forum in PHP. An alternative is to use an existing package, such as Phorum, to set up your forums. Web forums are sometimes also called discussion boards or threaded discussion groups.The idea of a forum is that people can post articles or questions to them, and others can read and reply to their questions. Each topic of discussion in a forum is called a thread. We will implement a Web forum called blah-blah with the following functionality. Users will be able to n Start new threads of discussion by posting articles n Post articles in reply to existing articles n View articles that have been posted n View the threads of conversation in the forum n View the relationship between articles, that is, see which articles are replies to other articles The Problem Setting up a forum is actually quite an interesting problem.We will need some way of storing the articles in a database with author, title, date, and content information.At first glance this might not seem much different from the Book-O-Rama database. However, the way most threaded discussion software works is that, along with show- ing you the available articles, it will show you the relationship between articles.That is, you are able to see which articles are replies to other articles (and which article they’re following up) and which articles are new topics of discussion. You can see examples of discussion boards that implement this in many places, including Slashdot: http://slashdot.org 35 525x ch29 1/24/03 3:36 PM Page 675 676 Chapter 29 Building Web Forums Deciding how to display these relationships will require some careful thought. For this system, a user should be able to view an individual message, a thread of conversation with the relationships shown, or all the threads on the system. Users must also be able to post new topics or replies.This is the easy part. Solution Components As we’ve said previously, storing and retrieving the author and text of a message is easy. The most difficult part of this application is finding a database structure that will store the information we want, and a way of navigating that structure efficiently. The structure of articles in a discussion might look like the one shown in Figure 29.1. Initial posting Reply 1 Reply 1 to Reply 1 Reply 2 Reply 3 Reply 1 to Reply 3 Reply 2 to Reply 1 Figure 29.1 An article in a threaded discussion might be the first article in a new topic, but more commonly it is a response to another article. In this diagram, you can see that we have an initial posting starting off a topic, with three replies. Some of the replies have replies.These replies could have replies, and so on. Looking at the diagram gives us a clue as to how we can store and retrieve the article data and the links between articles.This diagram shows a tree structure. If you’ve done much programming, you’ll know that this is one of the staple data structures used. In the diagram there are nodes—or articles—and links—or relationships between articles—just as in any tree structure. (If you are not familiar with trees as a data structure, don’t worry—we will cover the basics as we go.) The tricks to getting this all to work are 1. Finding a way to map this tree structure into storage—in our case, into a MySQL database 2. Finding a way to reconstruct the data as required We will begin by implementing a MySQL database that will enable us to store articles between use. 35 525x ch29 1/24/03 3:36 PM Page 676 . discussion groups and product tech- nical support. In this chapter, we implement a Web forum in PHP. An alternative is to use an existing package, such as Phorum, to set up your forums. Web forums are. calling mail() many times in PHP. Of course you could still build the front end in PHP, but have exmlm handle the grunt work. Next In the next chapter, we will implement a Web forum application that. or questions to them, and others can read and reply to their questions. Each topic of discussion in a forum is called a thread. We will implement a Web forum called blah-blah with the following