Chapter 11: Sending E - mail 331 message TEXT, PRIMARY KEY (email_id) ) ENGINE=MyISAM’; mysql_query($query, $db) or die (mysql_error($db)); echo ‘Success!’; ? > 2. Run db_ch10 - 2.php , and you should see the success message displayed. 3. Open up postcard.php in your editor and replace its content with the following code: < ?php require ‘db.inc.php’; $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); ? > < html > < head > < title > Send Postcard < /title > < script type=”text/javascript” > window.onload = function() { // assign change_postcard_image to select field var s = document.getElementById(‘postcard_select’); s.onchange = change_postcard_image; } function change_postcard_image() { var s = document.getElementById(‘postcard_select’); var i = document.getElementById(‘postcard’); var x = s.options.selectedIndex; // update image’s src and alt attributes i.src = s.options[x].value; i.alt = s.options[x].text; } < /script > < /head > < body > < h1 > Send Postcard < /h1 > < form method=”post” action=”sendconfirm.php” > < table > < tr > < td > Sender’s Name: < /td > < td > < input type=”text” name=”from_name” size=”40” / > < /td > < /tr > < /tr > < td > Sender’s E-mail: < /td > < td > < input type=”text” name=”from_email” size=”40” / > < /td > c11.indd 331c11.indd 331 12/10/08 6:05:30 PM12/10/08 6:05:30 PM 332 Part II: Comic Book Fan Site < /tr > < tr > < td > Recipient’s Name: < /td > < td > < input type=”text” name=”to_name” size=”40” / > < /td > < /tr > < /tr > < td > Recipient’s E-mail: < /td > < td > < input type=”text” name=”to_email” size=”40” / > < /td > < /tr > < tr > < td > Choose a Postcard: < /td > < td > < select id=”postcard_select” name=”postcard” > < ?php $query = ‘SELECT image_url, description FROM pc_image ORDER BY description’; $result = mysql_query($query, $db) or die(mysql_error()); $row = mysql_fetch_assoc($result); extract($row); mysql_data_seek($result, 0); while ($row = mysql_fetch_assoc($result)) { echo ‘ < option value=”’ . $row[‘image_url’] . ‘” > ’ . $row[‘description’] . ‘ < /option > ’; } mysql_free_result($result); ? > < /select > < /td > < /tr > < tr > < td colspan=”2” > < img id=”postcard” src=” < ?php echo $image_url; ? > ” alt=” < ?php echo $description; ? > ” / > < /td > < /tr > < tr > < td > Subject: < /td > < td > < input type=”text” name=”subject” size=”80” / > < /td > < /tr > < tr > < td colspan=”2” > < textarea cols=”76” rows=”12” name=”message” > Enter your message here < /textarea > < /td > < /tr > < tr > < td colspan=”2” > < input type=”submit” value=”Send” / > < input type=”reset” value=”Reset the form” / > < /td > < /tr > < /table > < /form > < /body > < /html > c11.indd 332c11.indd 332 12/10/08 6:05:31 PM12/10/08 6:05:31 PM Chapter 11: Sending E - mail 333 4. Next, write sendconfirm.php , the page that sends out the confirmation e - mail to the user. < ?php require ‘db.inc.php’; $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); $to_name = $_POST[‘to_name’]; $to_email = $_POST[‘to_email’]; $from_name = $_POST[‘from_name’]; $from_email = $_POST[‘from_email’]; $postcard = $_POST[‘postcard’]; $subject = $_POST[‘subject’]; $message = $_POST[‘message’]; $query = ‘SELECT description FROM pc_image WHERE image_url = “’ . $postcard . ‘”’; $result = mysql_query($query, $db) or die(mysql_error()); $description = ‘’; if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $description = $row[‘description’]; } mysql_free_result($result); $token = md5(time()); $query = ‘INSERT INTO pc_confirmation (email_id, token, to_name, to_email, from_name, from_email, subject, postcard, message) VALUES (NULL, “’ . $token . ‘”, “’ . $to_name . ‘”, “’ . $to_email . ‘”, “’ . $from_name . ‘”, “’ . $from_email . ‘”, “’ . $subject . ‘”, “’ . $postcard . ‘”, “’ . $message . ‘”)’; mysql_query($query, $db) or die(mysql_error()); $email_id = mysql_insert_id($db); $headers = array(); $headers[] = ‘MIME-Version: 1.0’; $headers[] = ‘Content-type: text/html; charset=”iso-8859-1”’; $headers[] = ‘Content-Transfer-Encoding: 7bit’; $headers[] = ‘From: no-reply@localhost’; $confirm_subject = ‘Please confirm your postcard [‘ . $subject .’]’; $confirm_message = ‘ < html > ’; $confirm_message .= ‘ < p > Hello, ‘ . $from_name . ‘. Please click on the link ‘ . ‘below to confirm that you would like to send this postcard. < /p > ’; $confirm_message .= ‘ < p > < a href=”http://localhost/confirm.php?id=’ . $email_id . ‘ & token=’ . $token .’” > Click here to confirm < /a > < /p > ’; c11.indd 333c11.indd 333 12/10/08 6:05:31 PM12/10/08 6:05:31 PM 334 Part II: Comic Book Fan Site $confirm_message .= ‘ < hr / > ’; $confirm_message .= ‘ < img src=”’ . $postcard . ‘” alt=”’ . $description . ‘ “/ > < br/ > ’; $confirm_message .= $message . ‘ < /html > ’; ? > < html > < head > < title > Mail Sent! < /title > < /head > < body > < ?php $success = mail($from_email, $confirm_subject, $confirm_message, join(“\r\n”, $headers)); if ($success) { echo ‘ < h1 > Pending Confirmation! < /h1 > ’; echo ‘ < p > A confirmation e-mail has been sent to ‘ . $from_email . ‘. ‘ . ‘Open your e-mail and click on the link to confirm that you ‘ . ‘would like to send this postcard to ‘ . $to_name . ‘. < /p > ’; } else { echo ‘ < p > < strong > There was an error sending the confirmation. < /strong > < /p > ’; } ? > < /body > < /html > 5. Next is confirm.php . This file is loaded in the browser with an ID in the URL to designate which saved postcard is awaiting confirmation, and the script then sends the postcard to the intended recipient. < ?php require ‘db.inc.php’; $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); $id = (isset($_GET[‘id’])) ? $_GET[‘id’] : 0; $token = (isset($_GET[‘token’])) ? $_GET[‘token’] : ‘’; $query = ‘SELECT email_id, token, to_name, to_email, from_name, from_email, subject, postcard, message FROM pc_confirmation WHERE token = “’ . $token . ‘”’; $result = mysql_query($query, $db) or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo ‘ < p > Oops! Nothing to confirm. < /p > ’; mysql_free_result($result); exit; } else { $row = mysql_fetch_assoc($result); extract($row); mysql_free_result($result); c11.indd 334c11.indd 334 12/10/08 6:05:31 PM12/10/08 6:05:31 PM Chapter 11: Sending E - mail 335 } $boundary = ‘==MP_Bound_xyccr948x==’; $headers = array(); $headers[] = ‘MIME-Version: 1.0’; $headers[] = ‘Content-type: multipart/alternative; boundary=”’ . $boundary . ‘”’; $headers[] = ‘From: ‘ . $from_email; $postcard_message = ‘ < html > ’; $postcard_message .= ‘ < p > Greetings, ‘ . $to_name . ‘! ‘; $postcard_message .= $from_name . ‘ has sent you a postcard today. < /p > ’; $postcard_message .= ‘ < p > Enjoy! < /p > ’; $postcard_message .= ‘ < hr / > ’; $postcard_message .= ‘ < img src=”’ . $postcard . ‘” alt=”’ . $description . ‘ “/ > < br/ > ’; $postcard_message .= $message; $postcard_message .= ‘ < hr/ > < p > You can also visit ‘ . ‘ < a href=”http://localhost/viewpostcard.php?id=’ . $email_id . ‘ & token=’ . $token .’” > http://localhost/viewpostcard.php?id=’ . $email_id . ‘ & token=’ . $token .’ < /a > to view this postcard online. < /p > < /html > ’; $mail_message = ‘This is a Multipart Message in MIME format’ . “\n”; $mail_message .= ‘ ’ . $boundary . “\n”; $mail_message .= ‘Content-type: text/html; charset=”iso-8859-1”’ . “\n”; $mail_message .= ‘Content-Transfer-Encoding: 7bit’ . “\n\n”; $mail_message .= $postcard_message . “\n”; $mail_message .= ‘ ’ . $boundary . “\n”; $mail_message .= ‘Content-Type: text/plain; charset=”iso-8859-1”’ . “\n”; $mail_message .= ‘Content-Transfer-Encoding: 7bit’ . “\n\n”; $mail_message .= strip_tags($postcard_message) . “\n”; $mail_message .= ‘ ’ . $boundary . ‘ ’ . “\n”; ? > < html > < head > < title > Postcard Sent! < /title > < /head > < body > < ?php $success = mail($to_email, $subject, $mail_message, join(“\r\n”, $headers)); if ($success) { echo ‘ < h1 > Congratulations! < /h1 > ’; echo ‘ < p > The following postcard has been sent to ‘ . $to_name . ‘: < br/ > < /p > ’; echo $postcard_message; } else { echo ‘ < p > < strong > There was an error sending your message. < /strong > < /p > ’; } ? > < /body > < /html > c11.indd 335c11.indd 335 12/10/08 6:05:32 PM12/10/08 6:05:32 PM 336 Part II: Comic Book Fan Site 6. Next, you ’ ll create a form that allows a user to view the postcard. Call this one viewpostcard.php . < ?php require ‘db.inc.php’; $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); $id = (isset($_GET[‘id’])) ? $_GET[‘id’] : 0; $token = (isset($_GET[‘token’])) ? $_GET[‘token’] : ‘’; $query = ‘SELECT email_id, token, to_name, to_email, from_name, from_email, subject, postcard, message FROM pc_confirmation WHERE token = “’ . $token . ‘”’; $result = mysql_query($query, $db) or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo ‘ < p > Oops! Nothing to view. < /p > ’; mysql_free_result($result); exit; } else { $row = mysql_fetch_assoc($result); extract($row); mysql_free_result($result); } ? > < html > < head > < title > < ?php echo $subject; ? > < /title > < /head > < body > < ?php echo ‘ < img src=”’ . $postcard . ‘” alt=”’ . $description . ‘ “/ > < br/ > ’; echo $message; ? > < /body > < /html > 7. Load postcard.php in your browser to verify that it works. The results should look similar to what ’ s shown in Figure 11 - 6 . c11.indd 336c11.indd 336 12/10/08 6:05:32 PM12/10/08 6:05:32 PM Chapter 11: Sending E - mail 337 Figure 11-6 c11.indd 337c11.indd 337 12/10/08 6:05:32 PM12/10/08 6:05:32 PM 338 Part II: Comic Book Fan Site 8. Enter the appropriate information; remember to put in valid e - mail addresses in the Sender ’ s E - mail and Recipient ’ s E - mail fields. 9. In the Choose a Postcard field, select a postcard from the drop - down list, enter a message, and click the Send button. A screen similar to the one shown in Figure 11 - 7 loads. Figure 11-7 10. Check your e - mail. You should receive an e - mail that looks something like Figure 11 - 8 . c11.indd 338c11.indd 338 12/10/08 6:05:33 PM12/10/08 6:05:33 PM Chapter 11: Sending E - mail 339 11. Click the link in the e - mail to confirm that you want to send the postcard. 12. Open the e - mail account this postcard was sent to (see Figure 11 - 9 ). You did send it to an e - mail address you have access to, right? If you sent this to your little sister, we sure hope you didn ’ t scare her! Figure 11-8 c11.indd 339c11.indd 339 12/10/08 6:05:33 PM12/10/08 6:05:33 PM 340 Part II: Comic Book Fan Site Figure 11-9 How It Works Your application is getting more complex. However, it is still fairly basic in the functionality it offers. Here ’ s what it does: The user loads postcard.php and fills out all the fields. He or she also selects a postcard to be sent. In the Sender ’ s E - mail field, the user enters his or her e - mail address. ❑ c11.indd 340c11.indd 340 12/10/08 6:05:33 PM12/10/08 6:05:33 PM [...]... pointer back to its beginning with mysql_ data_seek(), before generating the options for the select element . ‘db.inc.php’; $db = mysql_ connect (MYSQL_ HOST, MYSQL_ USER, MYSQL_ PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_ select_db (MYSQL_ DB, $db) or die (mysql_ error($db)); ?. ‘db.inc.php’; $db = mysql_ connect (MYSQL_ HOST, MYSQL_ USER, MYSQL_ PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_ select_db (MYSQL_ DB, $db) or die (mysql_ error($db)); . ‘db.inc.php’; $db = mysql_ connect (MYSQL_ HOST, MYSQL_ USER, MYSQL_ PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_ select_db (MYSQL_ DB, $db) or die (mysql_ error($db));