#native_company# #native_desc#
#native_cta#

Downloading and Parsing Gmail Messages in PHP Page 2

By Rose Kelleher
on August 31, 2010

imap_num_msg()

This function returns the number of messages in the mailbox, which is useful for looping.

   $n = imap_num_msg($mbox);

imap_header_info()

This function returns an object containing header information for a given message. It takes the mailbox object and the message number as parameters. The message numbers start with 1, so your loop might look something like this:

for ($i = 1; $i 

When you have the object, you can get the header information from its properties, such as from, date and subject.

imap_body()

This function returns the body of the message as a string. Like imap_header_info(), it takes the mailbox object and the message number as parameters.

$body = imap_body($mbox, $i);

imap_close()

This function closes the IMAP stream. It takes the mailbox as a parameter.

imap_close($mbox);

Where to Go from Here

Within your loop you can process individual messages. The example script in the first section ($mbox = imap_open($mailbox, $username, $password);) simply echoes the message contents, but if you know how the message body is formatted you can parse it and do something more useful with the information. If you really want to get fancy you can work with multipart MIME-encoded messages using imap_fetchstructure() and imap_fetchbody().
You’ll also should implement better security. I know you know this already, but I’ll make it the last word anyway: don’t hardcode your Gmail username and password in your script and then upload your script to a location where anyone can run it.

About the Author

Rose Kelleher is a freelance writer. to e-mail her.

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Rose Kelleher

Comment:



Comment: