1. Trang chủ
  2. » Công Nghệ Thông Tin

Phát triển web với PHP và MySQL - p 69 pps

10 185 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

CHAPTER 28 Building a Mailing List Manager 34 7842 CH28 3/6/01 3:46 PM Page 655 After you’ve built up a base of subscribers to your Web site, it’s nice to be able to keep in touch with them by sending out a newsletter. In this chapter, we will implement a front end for a mailing list manager (or MLM). Some MLMs allow each subscriber to send messages to other subscribers. Our program will be a newsletter system, in which only the list administrator can send messages. We will call our system Pyramid-MLM. This system will be similar to others already in the marketplace. To get some idea of what we are aiming for, take a look at http://www.topica.com Our application will let an administrator create multiple mailing lists and send newsletters to each of those lists separately. This application will use file upload to enable an administrator to upload text and HTML versions of newsletters that they have created offline. This means administrators can use whatever software they prefer to create newsletters. Users will be able to subscribe to any of the lists at our site and select whether to receive newsletters in text or HTML. The Problem We want to build an online newsletter composition and sending system. This system should allow various newsletters to be created and sent to users, and allow users to subscribe to one or many of the newsletters. Specifically, the requirements for this system are • Administrators should be able to set up and modify mailing lists. • Administrators should be able to send text and HTML newsletters to all the subscribers of a single mailing list. • Users should be able to register to use the site, and enter and modify their details. • Users should be able to subscribe to any of the lists on a site. • Users should be able to unsubscribe from lists they are subscribed to. • Users should be able to store their preference for either HTML formatted or plain text newsletters. • For security reasons, users should not be able to send mail to the lists or to see each other’s email addresses. • Users and administrators should be able to view information about mailing lists. • Users and administrators should be able to view past newsletters that have been sent to a list (the archive). Building Practical PHP and MySQL Projects P ART V 656 34 7842 CH28 3/6/01 3:46 PM Page 656 Solution Components There are a number of components we will need to fulfil the requirements. The main ones are setting up a database of lists, subscribers, and archived newsletters; uploading newsletters that have been created offline; and sending mail with attachments. Setting Up a Database of Lists and Subscribers We will track the username and password of each system user, as well as a list of the lists they have subscribed to. We will also store each user’s preference for receiving text or HTML email, so we can send a user the appropriate version of the newsletter. An administrator will be a specialized user with the ability to create new mailing lists and send newsletters to those lists. A nice piece of functionality to have for a system like this is an archive of previous newslet- ters. Subscribers might not keep previous postings, but might want to look something up. An archive can also act as a marketing tool for the newsletter as potential subscribers can see what the newsletters are like. Setting up this database in MySQL and an interface to it in PHP will have nothing new or difficult in it. File Upload We need an interface to allow the administrator to send newsletters, as mentioned previously. What we haven’t talked about is how administrators will create that newsletter. We could pro- vide them with a form where they could type or paste the newsletter content. However, it will increase the user-friendliness of our system to let administrators create a newsletter in their favorite editor and then upload the file to the Web server. This will also make it easy for an administrator to add images to an HTML newsletter. For this we can use the file upload capability discussed in Chapter 16, “Interacting with the File System and the Server.” We will need to use a slightly more complicated form than we have used in the past. We will require the administrator to upload both text and HTML versions of the newsletter, along with any inline images that go into the HTML. When the newsletter has been uploaded, we need to create an interface so that the administra- tor can preview the newsletter before sending it. This way, he can confirm that all the files were uploaded correctly. Building a Mailing List Manager C HAPTER 28 28 BUILDING A MAILING LIST MANAGER 657 34 7842 CH28 3/6/01 3:46 PM Page 657 Sending Mail with Attachments For this project, we would like to be able to send users either a plain text newsletter or a “fancy” HTML version, according to their preference. To send an HTML file with embedded images, we will need to find a way to send attachments. PHP’s simple mail() function doesn’t easily support sending attachments. Instead, we will use the excellent HTML MIME Mail class created by Richard Heyes. This can deal with HTML attachments, and will automatically attach any images that are contained in the HTML file. You can get the most up-to-date version of this class from http://www.heyes-computing.net/scripts/ (It’s also on the CD-ROM in this book.) You are free to use this script in your own work. It is released as Postcard-Ware. If you use it, send the author a post card. The address is on his Web site. Solution Overview For this project, we will again use an event-driven approach to writing our code, as we did in Chapter 27, “Building a Web-Based Email Service.” We have again begun by drawing a set of system flow diagrams to show the paths users might take through the system. In this case, we have drawn three diagrams to represent the three dif- ferent sets of interactions users can have with the system. Users have different allowable actions when they are not logged in, when they are logged in as regular users, and when they are logged in as administrators. These actions are shown in Figures 28.1, 28.2, and 28.3, respectively. Building Practical PHP and MySQL Projects P ART V 658 Show all lists Not logged in Login New Account FIGURE 28.1 A user can only choose a limited number of actions when he is not logged in. 34 7842 CH28 3/6/01 3:46 PM Page 658 In Figure 28.1 you can see the actions that can be taken by a user who is not logged in. As you can see, he can log in (if he already has an account), create an account (if he doesn’t already have one), or view the mailing lists available for signup (as a marketing tactic). Building a Mailing List Manager C HAPTER 28 28 BUILDING A MAILING LIST MANAGER 659 Not logged in Change Password Subscribe Show other lists Archive Show my lists Info Account Settlings Unsub FIGURE 28.2 After logging in, users can change their preferences through a variety of options. Figure 28.2 shows the actions a user can take after logging in. He can change his account set-up (email address and preferences), change his password, and change which lists he is subscribed to. Admin logged in Change Password Show other lists Show all lists Show my lists SubscribeArchiveInfoUnsub Create lists View Mail Send Mail Create Mail FIGURE 28.3 Administrators have additional actions available to them. Figure 28.3 shows the actions available if an administrator has logged in. As you can see, an administrator has most of the functionality available to a user, and some additional options. She can also create new mailing lists, create new messages for a mailing list by uploading files, and preview messages before sending them. 34 7842 CH28 3/6/01 3:46 PM Page 659 Because we have used an event-driven approach again, the backbone of the application is con- tained in one file, index.php, which calls on a set of function libraries. An overview of the files in this application is shown in Table 28.1. TABLE 28.1 Files in the Mailing List Manager Application Filename Type Description index.php Application The main script that runs the entire application. include_fns.php Functions Collection of include files for this application. data_valid_fns.php Functions Collection of functions for validating input data. db_fns.php Functions Collection of functions for connecting to the mlm database. mlm_fns.php Functions Collection of functions spe- cific to this application. output_fns.php Functions Collection of functions for outputting HTML. upload.php Component Script that manages the file upload component of the administrator role. Sepa- rated out to make security easier. user_auth_fns.php Functions Collection of functions for authenticating users. create_database.sql SQL SQL to set up the mlm data- base and set up a Web user and an administrative user. We will work our way through the project implementation, beginning with the database in which we will store subscriber and list information. Setting Up the Database For this application we will need to store details of • Lists: Mailing lists available for subscription. • Subscribers: Users of the system and their preferences. Building Practical PHP and MySQL Projects P ART V 660 34 7842 CH28 3/6/01 3:46 PM Page 660 • Sub_lists: A record of which users have subscribed to which lists (a many-to-many relationship) • Mail: A record of email messages that have been sent. • Images: Because we want to be able to send email messages that consist of multiple files (that is, text and HTML plus a number of images), we also need to track which images go with each email. The SQL we have written to create this database is shown in Listing 28.1. LISTING 28.1 create_database.sql—SQL to Create the mlm Database create database mlm; use mlm; create table lists ( listid int auto_increment not null primary key, listname char(20) not null, blurb varchar(255) ); create table subscribers ( email char(100) not null primary key, realname char(100) not null, mimetype char(1) not null, password char(16) not null, admin tinyint not null ); # stores a relationship between a subscriber and a list create table sub_lists ( email char(100) not null, listid int not null ); create table mail ( mailid int auto_increment not null primary key, email char(100) not null, subject char(100) not null, listid int not null, status char(10) not null, Building a Mailing List Manager C HAPTER 28 28 BUILDING A MAILING LIST MANAGER 661 34 7842 CH28 3/6/01 3:46 PM Page 661 LISTING 28.1 Continued sent datetime, modified timestamp ); #stores the images that go with a particular mail create table images ( mailid int not null, path char(100) not null, mimetype char(100) not null ); grant select, insert, update, delete on mlm.* to mlm@localhost identified by ‘password’; insert into subscribers values (‘admin@localhost’, ‘Administrative User’, ‘H’, password(‘admin’), 1); Remember that you can execute this SQL by typing mysql -u root -p < create_database.sql You will need to supply your root password. (You could, of course, execute this script via any MySQL user with the appropriate privileges; we have just used root here for simplicity.) You should change the password for the mlm user and the administrator in your script before running it. Some of the fields in this database require a little further explanation, so let’s briefly run through them. The lists table contains a listid and listname. It also contains a blurb, which is a description of what the list is about. The subscribers table contains email addresses (email) and names (realname)of the sub- scribers. It also stores their password and a flag (admin) to indicate whether or not this user is an administrator. We will also store the type of mail they prefer to receive in mimetype. This can be either H for HTML or T for text. The sublists table contains email addresses (email) from the subscribers table and listids from the lists table. The mail table contains information about each email message that is sent through the system. It stores a unique id (mailid), the address the mail is sent from (email), the subject line of the Building Practical PHP and MySQL Projects P ART V 662 34 7842 CH28 3/6/01 3:46 PM Page 662 email (subject), and the listid of the list it has been sent to or will be sent to. The actual text or HTML of the message could be a large file, so we will store the archive of the actual mes- sages outside the database. We will also track some general status information: whether the message has been sent (status), when it was sent (sent), and a timestamp to show when this record was last modified (modified). Finally, we use the images table to track any images associated with HTML messages. Again, these images can be large, so we will store them outside the database for efficiency. Instead, we will track the mailid they are associated with, the path to the location where the image is actually stored, and the MIME type of the image (mimetype), for example, image/gif. The SQL shown previously also sets up a user for PHP to connect as, and an administrative user for the system. Script Architecture As in the last project, we have used an event-driven approach to this project. The backbone of the application is in the file index.php. This script has four main segments, which are 1. Preprocessing: Do any processing that must be done before headers can be sent. 2. Set up and send headers: Create and send the start of the HTML page. 3. Perform action: Respond to the event that has been passed in. As in our last example, the event is contained in the $action variable. 4. Send footers. Almost all of the application’s processing is done in this file. The application also uses the function libraries listed in Table 28.1, as mentioned previously. The full listing of the index.php script is shown in Listing 28.2. LISTING 28.2 index.php—Main Application File for Pyramid-MLM <? /********************************************************************** * Section 1 : pre-processing *********************************************************************/ include (‘include_fns.php’); session_start(); $buttons = array(); //append to this string if anything processed before header has output Building a Mailing List Manager C HAPTER 28 28 BUILDING A MAILING LIST MANAGER 663 34 7842 CH28 3/6/01 3:46 PM Page 663 LISTING 28.2 Continued $status = ‘’; // need to process log in or out requests before anything else if($email&&$password) { $login = login($email, $password); if($login == ‘admin’) { $status .= “<p><b>”.get_real_name($email).”</b> logged in” .” successfully as <b>Administrator</b><br><br><br><br><br>”; $admin_user = $email; session_register(“admin_user”); } else if($login == ‘normal’) { $status .= “<p><b>”.get_real_name($email).”</b> logged in” .” successfully.<br><br>”; $normal_user = $email; session_register(“normal_user”); } else { $status .= “<p>Sorry, we could not log you in with that email address and password.<br>”; } } if($action == ‘log-out’) { session_destroy(); unset($action); unset($normal_user); unset($admin_user); } /********************************************************************** * Section 2: set up and display headers *********************************************************************/ // set the buttons that will be on the tool bar if(check_normal_user()) { // if a normal user Building Practical PHP and MySQL Projects P ART V 664 34 7842 CH28 3/6/01 3:46 PM Page 664 . Manager Application Filename Type Description index .php Application The main script that runs the entire application. include_fns .php Functions Collection of include files for this application. data_valid_fns .php. Collection of functions spe- cific to this application. output_fns .php Functions Collection of functions for outputting HTML. upload .php Component Script that manages the file upload component of the administrator. the system. Script Architecture As in the last project, we have used an event-driven approach to this project. The backbone of the application is in the file index .php. This script has four main

Ngày đăng: 06/07/2014, 19:20

Xem thêm: Phát triển web với PHP và MySQL - p 69 pps