Archive for November, 2007

Space web hosting - Subject:

Friday, November 16th, 2007
Subject: width=320 height=240 border=0 id= postcard >  

4. Next write sendconf.php, the page that sends out the confirmation e-mail to the user. Remember that much of this code can be pulled (cannibalized) from sendmail.php. ; $html_msg .=

; 342 Chapter 11
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

address. Once you (Cpanel web hosting) get the confirmation, you know

Thursday, November 15th, 2007

address. Once you get the confirmation, you know the user entered a good e-mail address, and you can go ahead and send the e-mail. This act of achieving confirmation is the first step toward creating a workflow application. Workflow applications are covered in more detail in Chapter 13, but for now just understand that a workflow application is one that requires input from various parties before it reaches its final destination. To accommodate this workflow, your application must undergo a metamorphosis from what it was in the past. The sendmail.php script must be split into two separate processes such that, in between the two processes, you wait for confirmation. Much of the code you have used so far in sendmail.php will be recycled. If you are an experienced developer, we are sure you know very well how to cannibalize your old code! If you are not familiar with cannibalization, now is the time to learn. Sometimes you need to write a function that you have written before in another application. With a couple of modifications, it would work in your new app. So, you copy and paste it into your new application and make the necessary changes. Voila, your first cannibalization! To confirm an e-mail address, the postcard information needs to be temporarily stored in a table, to be retrieved later on, once confirmation has been established: Try It Out Getting Confirmation In this exercise, you ll implement the confirmation e-mail into your application. (You may want to save your old postcard.php and sendmail.php files and start new files from scratch before making this change.) 1. Open your favorite PHP editor and create a new PHP file called db_makeconfirm.php. Make sure you use the correct server, username, and password. confirm created. ?> 340 Chapter 11
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web site traffic - $sql = CREATE DATABASE postcard ; $success = mysql_query($sql,

Wednesday, November 14th, 2007

$sql = CREATE DATABASE postcard ; $success = mysql_query($sql, $conn) or die(mysql_error()); echo Database created. . . ; Now that the database is created, the script then selects that database. mysql_select_db( postcard , $conn); Next, you create the images table in the database, containing three columns. As before, if the creation is successful, the script prints images table created to the screen and moves on. $sql = CREATE TABLE images (id int NOT NULL primary key auto_increment, img_url VARCHAR(255) NOT NULL, img_desc text ) ; $success = mysql_query($sql, $conn) or die(mysql_error()); echo images table created. . . ; Next, you need to create two arrays of values to place in the images table. You need to know the location of the postcards entered previously in step 1 and their descriptions. Each URL corresponds to a description in the other array. $imgURL = array( punyearth.gif , grebnok.gif , sympathy.gif , congrats.gif ); $imgDESC = array( Wish you were here! , See you soon! , Our Sympathies , Congratulations! ); Next, the script loops through the arrays, pulling the location and description text and inserting them into the images table. If there are no errors, the script prints Data entered to the screen. for ($i=0; $i<4; $i++) { $sql = INSERT INTO images ( images.img_url , images.img_desc ) VALUES ( $imagepath$imgURL[$i] , $imgDESC[$i] ) ; $success = mysql_query($sql, $conn) or die(mysql_error()); } Echo Data entered. . . ; When you run this PHP script, if anything goes wrong, make sure you delete the postcard database before running it again. Otherwise, you will get an error (database exists) and the script will stop executing. You ll include the code to view and use the images in the next section. Getting Confirmation So far, you have a pretty cool postcard application. Any user can send a postcard to whomever he or she wants, and PHP takes care of mailing it. Unfortunately, there is still a small problem with the application. As the application stands right now, it is quite easy for the user to use any e-mail address in the From field. This is a bad thing because nasty e-mails can be sent on someone else s behalf, and you don t want that, do you? To prevent such maliciousness, you must first send a confirmation e-mail to the From 339 Sending E-mail
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

3. Save this (Post office web site) file as conn_comic.php in the

Tuesday, November 13th, 2007

3. Save this file as conn_comic.php in the includes/ folder. You can call it whatever you like, but you are going to be including it in a few files using the require() function, so you ll have to remember to use the filename you came up with. Or, you could go with the name we came up with, because we took the time to come up with such a clever name, and have used it in subsequent PHP pages. 4. Enter the following code, and save it as db_insertpics.php. If you used the four pictures we provided for you and put them in the postcards/ folder, you need not change this file. If you are using a different number of postcards, or have created your own, make sure you modify this code to reflect those changes. And, if you have named the conn_comic.php file something you think is more clever than our name, make sure you reflect that change here. How It Works First, the script connected to the server using the correct username and password. $conn = mysql_connect( localhost , bp5am , bp5ampass ); The next step is to create the database, called postcard. If the creation is successful, the script prints Database created to the screen and moves on. 338 Chapter 11
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Personal web server - The HTML portion of your e-mail follows. Note

Monday, November 12th, 2007

The HTML portion of your e-mail follows. Note the double dashes (–) in front of the boundary. Also note the use of two new lines (nn) on the Content-Transfer-Encoding line. Do not neglect those the code will not work correctly without them. $message .= –$boundaryn ; $message .= Content-type: text/html; charset=iso-8859-1n ; $message .= Content-Transfer-Encoding: 7bitnn ; $message .= $messagebody . n ; Next is the text portion of your e-mail. Note the similarity to the HTML portion. You do not need to include the same $messagebody here. In fact, you would usually include an alternate message in text format. $message .= –$boundaryn ; $message .= Content-Type: text/plain; charset= iso-8859-1 n ; $message .= Content-Transfer-Encoding: 7bitnn ; $message .= $messagebody . n ; This is the final boundary. Note the double dashes (–) at the end. This signifies that it s the end of the e-mail. $message .= –$boundary– ; Your boundary in this case was set by the following line: $boundary = ==MP_Bound_xyccr948x== ; Storing Images To create a postcard application, you need to have digital postcards available for the user to choose from. For the purposes of this example, you ll have four postcards. If you are ambitious, you can add more, and we hope that you will! Try It Out Storing Images Let s add some nice postcards to the application, shall we? You can create your own, or you can download the images from the Web site (www.wrox.com). 1. First, store your postcard images in a folder on your Apache server. We have ours in the folder postcards/. Place them anywhere you like, but remember where they are. 2. Start up your favorite PHP editor, and type the following code. Make sure you enter your own server, username, password, and database name: 337 Sending E-mail
You want to have a cheap webhost for your apache application, then check apache web hosting services.

How It (Web hosting provider) Works You added a couple of

Friday, November 9th, 2007

How It Works You added a couple of new lines to the variable $headers. This allows you to do many additional things with your e-mail, including adding HTML. This line is required in order to use extended MIME capabilities (such as HTML). $headers = MIME-Version: 1.0rn ; Note the rn. This is a carriage return and new line, which must be entered between each header. UNIX sometimes allows just n, but to be on the safe side, you should always use rn. The following indicates that you will be using HTML in your message: $headers .= Content-type: text/html; charset=iso-8859-1rn ; $headers .= Content-Transfer-Encoding: 7bitrn ; It is concatenated to the $headers variable. That s all there is to adding HTML to your messages. All you have to do is tell the e-mail client to expect HTML, and it will work. You can really get fancy now with tables, style sheets, images, and so on. However, there is still a concern what about e-mail programs that don t accept or recognize HTML? What happens to them? You certainly want this application to be as user-friendly as possible, right? Not to worry you ll take care of them with multipart (or mixed) messages. Multipart Messages You want to be able to send your postcards to anyone. However, some people don t have HTML capabilities in their e-mail client. Therefore, you will send your postcards using both plain text and HTML. Try It Out Multipart Messages To send messages with both plain text and HTML, you will use Multipart Messages. Here s how to do it: 1. Edit your copy of postcard.php in your favorite text editor. Back up postcard.php before making changes if you want to keep the old version. 2. Make the following modifications (shown highlighted) to postcard.php:

334 Chapter 11
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

$headers = MIME-Version: 1.0rn ; $headers .= Content-type: text/html; (Web page design)

Thursday, November 8th, 2007

$headers = MIME-Version: 1.0rn ; $headers .= Content-type: text/html; charset=iso-8859-1rn ; $headers .= Content-Transfer-Encoding: 7bitrn ; $headers .= From: . $from . rn ; $mailsent = mail($to, $subject, $message, $headers); if ($mailsent) { echo Congrats! The following message has been sent:

; echo To: $to
; echo From: $from
; echo Subject: $subject
; echo Message:
; echo $message; } else { echo There was an error… ; } ?> 3. Save the file. 4. Load postcard.php into your browser and fill in the fields with appropriate information. Be sure to include some HTML in the message field, such as

Hello World!

Prepare for our arrival.

We are starving!!! 5. Click the Send button, and open your e-mail client to see the new message, which will look something like Figure 11-5. Figure 11-5 333 Sending E-mail
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Figure 11-4 (Web hosting account) How It Works Perhaps this heading

Wednesday, November 7th, 2007

Figure 11-4 How It Works Perhaps this heading should be How It Doesn t Work. That s because your e-mail client does not know that it has received HTML. Why? Because you didn t tell it! In order for any HTML-capable client to display HTML, the client needs to be told that the incoming e-mail is going to have some HTML tags on it. Only then will it know how to properly display your message. Try It Out Sending HTML by Using Headers You need a way for your e-mail to tell the client it contains HTML. This is accomplished by using headers. You already saw how to use headers to include a From: parameter. Now you are going to use a similar header to tell the client that the e-mail message contains HTML. 1. Edit your copy of sendmail.php in your favorite text editor. Back up sendmail.php before making changes if you want to keep the old version. 2. Make the following highlighted modifications to this file: We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

The mail() function returns a value of True (My web server)

Wednesday, November 7th, 2007

The mail() function returns a value of True if it is successful and False if it fails. You can use this function to make your application a little more robust: $mailsent = mail($to, $subject, $message, $headers); if ($mailsent) { echo Congrats! The following message has been sent:

; echo To: $to
; echo From: $from
; echo Subject: $subject

; echo Message:
; echo $message; } else { echo There was an error… ; } ?> Of course, you can modify this to handle errors more elegantly. Use the knowledge you acquired in Chapter 9 to do so. You have now created your first PHP e-mail application. Congratulations! (Call your mother! She ll be so proud.) But you ll probably soon get tired of ugly, plain-text e-mails. I m sure you re champing at the bit to create colorful, formatted e-mails, right? How else are you going to enable users to send some pretty postcards? Okay, let s do something about that! Dressing Up Your E-mails with HTML Because you are creating a postcard application, sending plain-text e-mails just won t do. You want to dress them up a bit, and make them look professional, yet attractive. So, add a bit of HTML to your e-mail code to dress it up. Try It Out Sending HTML Code in an E-mail First, let s try a little experiment. This step isn t vital, but it will help illustrate a later point about headers. 1. Go to step 5 of the previous Try It Out section and send another e-mail. This time, put some HTML in the message. An example would be:

Hello World!

Prepare for our arrival.

We are starving!!! 2. When you have entered all relevant data in the form, click the Send button, and check your e-mail. It should look something like the e-mail shown in Figure 11-4. 331 Sending E-mail
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

7. Open your e-mail client and check your (Web site translator)

Tuesday, November 6th, 2007

7. Open your e-mail client and check your e-mail (which should look like the one shown in Figure 11-3). Figure 11-3 How It Works We assume that you know HTML well enough that we don t have to explain postcard.php in great detail. Just remember that if your sendmail.php page is not in the same folder as postcard.php, you have to provide the correct path:
Once the user presses the Send button, sendmail.php is loaded. The first step in your PHP code assigns all the fields from postcard.php to variables. This step is not necessary if register_globals is set to On in your php.ini file, but we strongly recommend you use this code anyway, in case register_ globals is ever turned Off: $to = $_POST[ to ]; $from = $_POST[ from ]; $subject = $_POST[ subject ]; $message = $_POST[ message ]; To specify from whom the e-mail is coming, use the optional fourth parameter for the mail() function, headers. Headers are explained in more detail in the section Sending HTML by Using Headers, later in this chapter. $headers = From: . $from . rn ; 330 Chapter 11
You want to have a cheap webhost for your apache application, then check apache web hosting services.


To:
From: