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 />

679<br />

Listing 29.10<br />

Continued<br />

$message['body'] = imap_body($imap, $messageid);<br />

if(!$message['body']) {<br />

$message['body'] = "[This message has no body]\n\n\n\n\n\n";<br />

}<br />

if($fullheaders) {<br />

$message['fullheaders'] = imap_fetchheader($imap, $messageid);<br />

} else {<br />

$message['fullheaders'] = '';<br />

}<br />

$message['subject'] = $header->subject;<br />

$message['fromaddress'] = $header->fromaddress;<br />

$message['toaddress'] = $header->toaddress;<br />

$message['ccaddress'] = $header->ccaddress;<br />

$message['date'] = $header->date;<br />

// note we can get more detailed information by using from <strong>and</strong> to<br />

// rather than fromaddress <strong>and</strong> toaddress, but these are easier<br />

imap_close($imap);<br />

return $message;<br />

}<br />

Again, you use open_mailbox() to open the user’s mailbox.This time, however, you are<br />

after a specific message. Using this function library, you download the message headers<br />

<strong>and</strong> message body separately.<br />

The three IMAP functions used here are imap_header(), imap_fetchheader(), <strong>and</strong><br />

imap_body(). Note that the two header functions are distinct from imap_headers(), the<br />

one used previously.They are somewhat confusingly named.To summarize<br />

n imap_headers()—Returns a summary of the headers for all the messages in a<br />

mailbox. It returns them as an array with one element per message.<br />

n imap_header()—Returns the headers for one specific message in the form of an<br />

object.<br />

n imap_fetchheader()—Returns the headers for one specific message in the form<br />

of a string.<br />

In this case, you use imap_header() to fill out specific header fields <strong>and</strong> imap_<br />

fetchheader() to show the user the full headers if requested. (We come back to this<br />

topic later.)<br />

You use imap_header() <strong>and</strong> imap_body() to build an array containing all the elements<br />

of a message that you are interested in.You call imap_header() as follows:<br />

$header = imap_header($imap, $messageid);

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

Saved successfully!

Ooh no, something went wrong!