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

PHP and MySQL Web Development - P128 ppt

5 118 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 120,15 KB

Nội dung

607 Reading Mail <select onchange="window.location=this.options[selectedIndex].value" name="account"> <option value="0" selected> Choose Account</a> <option value="index.php?action=select-account&account=10"> mail.domain.com </option> <option value="index.php?action=select-account&account=11"> mail.server.com </option> <option value="index.php?action=select-account&account=9"> localhost </option> </select> Most of this code is just an HTML select element, but it also includes a little JavaScript. In the same way that PHP can generate HTML, it can also be used to generate client- side scripts. Whenever a change event happens to this element, JavaScript will set window. location to the value of the option. If your user selects the first option in the select, window.location will be set to 'index.php?action=select-account&account=10'. This will result in this URL being loaded. Obviously, if the user has a browser that does not support JavaScript or has JavaScript disabled, this code will have no effect. The display_account_select() function, from output_fns.php, is used to get the available account list and display the SELECT. It also uses the get_account_list() func- tion we discussed previously. Choosing one of the options in the SELECT activates the select_account event. If you look at the URL in Figure 27.5, you can see this appended to the end of the URL, along with the account ID of the chosen account. This has two effects. First, in the preprocessing stage of index.php, the chosen account will be stored in the session variable $selected_account, as follows: case 'select-account' : { // if have chosen a valid account, store it as a session variable if($account&&account_exists($HTTP_SESSION_VARS['auth_user'], $account)) { $HTTP_SESSION_VARS['selected_account'] = $account; } } Second, when the body stage of the script is executed, the following code will be exe- cuted: case 'select-account' : case 'view-mailbox' : { 33 525x ch27 1/24/03 2:56 PM Page 607 608 Chapter 27 Building a Web-Based Email Service // if mailbox just chosen, or view mailbox chosen, show mailbox display_list($HTTP_SESSION_VARS['auth_user'], $HTTP_SESSION_VARS['selected_account']); break; } As you can see, we take the same action here as if the user had chosen the View Mailbox option.We’ll look at that next. Viewing Mailbox Contents Mailbox contents can be viewed with the display_list() function.This displays a list of all the messages in the mailbox.The code for this function is shown in Listing 27.8. Listing 27.8 display_list() Function from output_fns.php—Function to Display All Mailbox Messages function display_list($auth_user, $accountid) { // show the list of messages in this mailbox global $table_width; if(!$accountid) { echo 'No mailbox selected<br /><br /><br /><br /><br /><br />.'; } else { $imap = open_mailbox($auth_user, $accountid); if($imap) { echo "<table width=$table_width' cellspacing='0' cellpadding='6' border='0'>"; $headers = imap_headers($imap); // we could reformat this data, or get other details using // imap_fetchheaders, but this is not a bad summary so we just echo each $messages = sizeof($headers); for($i = 0; $i<$messages; $i++) { echo '<tr><td bgcolor = "'; if($i%2) echo '#ffffff'; 33 525x ch27 1/24/03 2:56 PM Page 608 609 Reading Mail else echo '#ffffcc'; echo '"><a href="index.php?action=view-message&messageid='.($i+1).'">'; echo $headers[$i]; echo "</a></td></tr>\n"; } echo '</table>'; } else { $account = get_account_settings($auth_user, $accountid); echo 'could not open mail box '.$account['server']. '.<br /><br /><br /><br />'; } } } In this function, we actually begin to use PHP’s IMAP functions.The two key parts of this function are opening the mailbox and reading the message headers. We open the mailbox for a user account with a call to the open_mailbox() function that we have written in mail_fns.php.This function is shown in Listing 27.9. Listing 27.9 open_mailbox() Function from mail_fns.php—This Function Connects to a User Mailbox function open_mailbox($auth_user, $accountid) { global $HTTP_SESSION_VARS; // select mailbox if there is only one if(number_of_accounts($auth_user)==1) { $accounts = get_account_list($auth_user); $HTTP_SESSION_VARS['selected_account'] = $accounts[0]; $accountid = $accounts[0]; } // connect to the POP3 or IMAP server the user has selected $settings = get_account_settings($auth_user, $accountid); if(!sizeof($settings)) return 0; $mailbox = '{'.$settings[server]; if($settings[type]=='POP3') $mailbox .= '/pop3'; $mailbox .= ':'.$settings[port].'}INBOX'; Listing 27.8 Continued 33 525x ch27 1/24/03 2:56 PM Page 609 610 Chapter 27 Building a Web-Based Email Service // suppress warning, remember to check return value @ $imap = imap_open($mailbox, $settings['remoteuser'], $settings['remotepassword']); return $imap; } We actually open the mailbox with the imap_open() function.This function has the fol- lowing prototype: int imap_open (string mailbox, string username, string password [, int options]) The parameters you need to pass to it are as follows: n mailbox—This string should contain the server name and mailbox name, and optionally a port number and protocol.The format of this string is {hostname/protocol:port}boxname If the protocol is not specified, it defaults to IMAP. In the code we have written, you can see that we specify POP3 if the user has specified that protocol for a par- ticular account. For example, to read mail from the local machine using the default ports, we would use the following mailbox name for IMAP: {localhost:143}INBOX and this one for POP3 {localhost/pop3:110}INBOX n username—The username for the account n password—The password for the account You can also pass it optional flags to specify options such as "open mailbox in read-only mode". One thing to note is that we have constructed the mailbox string piece by piece with the concatenation operator before passing it to imap_open().You need to be careful how you construct this string because strings containing {$ cause problems in PHP 4. This function call returns an IMAP stream if the mailbox can be opened, and false if it cannot. When you are finished with an IMAP stream, you can close it using imap_close(imap_stream). In our function, the IMAP stream is passed back to the main program.We then use the imap_headers() function to get the email headers for display: $headers = imap_headers($imap); Listing 27.9 Continued 33 525x ch27 1/24/03 2:56 PM Page 610 611 Reading Mail This function returns header information for all mail messages in the mailbox we have connected to.The information is returned as an array, one line per message.We haven’t formatted this. It just outputs one line per message, so you can see from looking at Figure 27.5 what the output looks like. You can get more information about email headers using the confusing, similarly named imap_header().In this case, the imap_headers() function gives us enough detail for our purpose. Reading a Mail Message We have set up each of the messages in the previous display_list() function to link to specific email messages. Each link is of the form index.php?action=view-message&messageid=6 The messageid is the sequence number used in the headers we retrieved earlier. Note that IMAP messages are numbered from 1, not 0. If the user clicks one of these links, he will see output like that shown in Figure 27.6. Figure 27.6 Using the view-message action shows us a particular mes- sage—in this case, it’s a piece of spam. When we enter these parameters into the index.php script, we execute the following code: 33 525x ch27 1/24/03 2:56 PM Page 611 . following code will be exe- cuted: case 'select-account' : case 'view-mailbox' : { 33 525x ch27 1/24/03 2:56 PM Page 607 608 Chapter 27 Building a Web- Based Email Service //. Account</a> <option value="index .php? action=select-account&account=10"> mail.domain.com </option> <option value="index .php? action=select-account&account=11"> mail.server.com </option> <option. display_account_select() function, from output_fns .php, is used to get the available account list and display the SELECT. It also uses the get_account_list() func- tion we discussed previously. Choosing

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

TỪ KHÓA LIÊN QUAN