RESTful. ASCII. Packages. XML. Retrieve Your Gmail Emails Using PHP and IMAP. Grabbing emails from your Gmail account using PHP is probably easier than you think. Armed with PHP and its IMAP extension, you can retrieve emails from your Gmail account in no time! Just for fun, I'll be using the MooTools Fx.Accordion plugin to display each email. The CSS Some simple CSS formatting. The MooTools JavaScript window.addEvent('domready',function() { var togglers = $$('div.toggler'); if(togglers.length) var gmail = new Fx.Accordion(togglers,$$('div.body')); togglers.addEvent('click',function() { this.addClass('read').removeClass('unread'); }); togglers[0].fireEvent('click');}); Some simple accordion code.
The PHP With our individual username/password settings set, we connect to Gmail. Now that I've shown you the basics, it's time for you to build your next great PHP email app! There are some encoding issues and I've not found a great way to include images. Be Heard Tip: Wrap your code in <pre> tags or link to a GitHub Gist! Newer Introducing MooTools Dotter. Imap_search. Imap_search — This function returns an array of messages matching the given search criteria Description ¶ array imap_search ( resource $imap_stream , string $criteria [, int $options = SE_FREE [, string $charset = NIL ]] ) For example, to match all unanswered messages sent by Mom, you'd use: "UNANSWERED FROM mom".
Searches appear to be case insensitive. This list of criteria is from a reading of the UW c-client source code and may be incomplete or inaccurate (see also » RFC2060, section 6.4.4). Parameters ¶ imap_stream An IMAP stream returned by imap_open(). criteria A string, delimited by spaces, in which the following keywords are allowed. Options Valid values for options are SE_UID, which causes the returned array to contain UIDs instead of messages sequence numbers. charset Return Values ¶ Returns an array of message numbers or UIDs. Return FALSE if it does not understand the search criteria or no messages have been found. Changelog ¶ Examples ¶ Example #1 imap_search() example See Also ¶ September 28 blog – Dan Spencer. PHP IMAP support in Mac OS X Lion without recompiling PHP OK, so this was a lot harder than it should have been, and I had to use various tutorials to get it working, however I am pleased to say that I finally did get it all working without needing to completely recompile php.
Here are the steps: First, modified from Download IMAP source code (now 2007f).Extract the tar.gz file (double click it in finder or tar xvzf imap-2007f.tar.gz in terminal)Then, modified from This Comment on the same xeonxai blog post, do the following to avoid the error:"configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. So that’s it, hope it helps! Dan Spencer Computing, OS X imap, lion, mac os x, pcre, php Fixing mysql in OS X Lion upgrade After upgrading to OS X Lion today, MySQL seemed to stop working. Php not connecting to mysql servermysql no longer in path (terminal commands “mysql -u root -p” doesn’t work) Writing a mail client that handles attachments with PHP. When you think about writing a mail client in PHP, beware it's fuzzy because of missing documentation on the imap functions in PHP.
PHP uses the imap functions to fetch and handle mailboxes (also POP3) just a matter of how you connect to your mailaccount. Making the connection Now we've got a connection to the mailbox. To retrieve some content from a mailbox we now always use the object $mbox which has many features and almost any of these are more or less documented. You can always find information about it on www.php.net search in functions for imap. Now to an example on how to retrieve a list of all messages: Message list echo "</table>"; imap_close($mbox); ? This produces a message list where you can read the subject and the date and time for arrival in you mailbox. The main PHP function used is the imap_fetch_overview($mbox,"1:$MN",0); in this example it tells PHP to fetch message 1 to $MN ($msgCount = the last message from the connection $mbox).
Body retrieval <? <? Attachment handling. Imap - Extract body text from Email PHP. Imap_fetchbody. The function imap_fetchbody() seems uncapable of getting subordinate parts of a message/rfc822-part. I had problems with getting an attachment that was forwarded in such a part, because the object given by imap_fetchstructure() would assume that the part was represented by the string "2.1.2.1.2". So I wrote this set of functions which parses the raw message-body and creates an array with the struture corresponding to the structure given by imap_fetchstructure().
The function mail_fetchpart() (see below) will work on the array and return even those parts that I could not get with imap_fetchbody(). Example usage of this function: mail_fetchpart($mbox, 2, "2.1.2.1.2"); Note: If the message does not contain multiple pars, the body of the message can be accessed by the part-string "1". I have more functions for parsing and decoding messages, just email me. if (eregi ($PN_EREG_BOUNDARY, $header, $regs)) { $boundary = $regs[2];