13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution Overview<br />

689<br />

content. However, it increases the user-friendliness of the system to let the administrator<br />

create a newsletter in his favorite editor <strong>and</strong> then upload the file to the web server.This<br />

also makes it easy for the administrator to add images to an HTML newsletter. For this,<br />

you can use the file upload capability discussed in Chapter 19,“Interacting with the File<br />

System <strong>and</strong> the Server.”<br />

You need to use a slightly more complicated form than you used in previous projects.<br />

For this project, you require the administrator to upload both text <strong>and</strong> HTML versions<br />

of the newsletter, along with any inline images that go into the HTML.<br />

After the newsletter has been uploaded, you need to create an interface so that the<br />

administrator can preview the newsletter before sending it.This way, he can confirm that<br />

all the files were uploaded correctly.<br />

Note that you also store all these files in an archive directory so that users can read<br />

back issues of newsletters.This directory needs to be writable by the user your web server<br />

runs as.The upload script will try to write the newsletters into ./archive/, so you<br />

need to make sure you create that directory <strong>and</strong> set permissions on it appropriately.<br />

Sending Mail with Attachments<br />

For this project, you want to be able to send users either a plain-text newsletter or a<br />

“fancy” HTML version, according to their preference.<br />

To send an HTML file with embedded images, you need to find a way to send<br />

attachments. <strong>PHP</strong>’s simple mail() function doesn’t easily support sending attachments.<br />

Instead, you can use the excellent Mail_Mime package from PEAR, originally created by<br />

Richard Heyes. It can deal with HTML attachments <strong>and</strong> can also be used to attach any<br />

images that are contained in the HTML file.<br />

Installation instructions for this package are included under “Installing PEAR” in<br />

Appendix A,“Installing <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong>.”<br />

Solution Overview<br />

For this project, you again use an event-driven approach to writing the code, as in<br />

Chapter 29,“Building a <strong>Web</strong>-Based Email Service.”<br />

To help you get started, we again began by drawing a set of system flow diagrams to<br />

show the paths users might take through the system. In this case, we drew three diagrams<br />

to represent the three different sets of interactions users can have with the system. Users<br />

have different allowable actions when they are not logged in, when they are logged in as<br />

regular users, <strong>and</strong> when they are logged in as administrators.These actions are shown in<br />

Figures 30.1, 30.2, <strong>and</strong> 30.3, respectively.<br />

In Figure 30.1, you can see the actions that can be taken by a user who is not logged<br />

in. As you can see, he can log in (if he already has an account), create an account (if he<br />

doesn’t already have one), or view the mailing lists available for signup (as a marketing<br />

tactic).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!