13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Reading Mail<br />

675<br />

Listing 29.8<br />

Continued<br />

}<br />

}<br />

}<br />

echo "Could not open mail<br />

box ".$account['server'].".";<br />

In the display_list() function, you actually begin to use <strong>PHP</strong>’s IMAP functions.The<br />

two key parts of this function are opening the mailbox <strong>and</strong> reading the message headers.<br />

You open the mailbox for a user account with a call to the open_mailbox() function<br />

written in mail_fns.php.This function is shown in Listing 29.9.<br />

Listing 29.9 open_mailbox() Function from mail_fns.php—This Function<br />

Connects to a User Mailbox<br />

function open_mailbox($auth_user, $accountid) {<br />

// select mailbox if there is only one<br />

if(number_of_accounts($auth_user)==1) {<br />

$accounts = get_account_list($auth_user);<br />

$_SESSION['selected_account'] = $accounts[0];<br />

$accountid = $accounts[0];<br />

}<br />

// connect to the POP3 or IMAP server the user has selected<br />

$settings = get_account_settings($auth_user, $accountid);<br />

if(!sizeof($settings)) {<br />

return 0;<br />

}<br />

$mailbox = '{'.$settings[server];<br />

if($settings[type]=='POP3') {<br />

$mailbox .= '/pop3';<br />

}<br />

$mailbox .= ':'.$settings[port].'}INBOX';<br />

// suppress warning, remember to check return value<br />

@$imap = imap_open($mailbox, $settings['remoteuser'],<br />

$settings['remotepassword']);<br />

return $imap;<br />

}<br />

You actually open the mailbox with the imap_open() function, which has the following<br />

prototype:<br />

int imap_open (string mailbox, string username, string password [, int options])

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

Saved successfully!

Ooh no, something went wrong!