- /* connect to gmail */
- $hostname = ‘{imap.gmail.com:993/imap/ssl}’;
- $mailbox = ‘&ZcWITHb4UXM-‘;
- $username = ’emailaddress@gmail.com’;
- $password = ’emailpassword’;
- /* try to connect */
- $inbox = imap_open($hostname . $mailbox, $username, $password) or die(‘Cannot connect to Gmail: ‘ . imap_last_error());
- /* grab emails */
- $emails = imap_search($inbox, ‘ALL’);
- //$emails = imap_search($inbox, ‘SUBJECT “Gmail”‘);
- /* if emails are returned, cycle through each… */
- if ($emails) {
- /* begin output var */
- $output = ”;
- /* put the newest emails on top */
- rsort($emails);
- /* for every email… */
- foreach ($emails as $num) {
- // $sum++;
- // if ($sum > 3)
- // break;
- /* get information specific to this email */
- // $header = imap_header($inbox, $num);
- $overview = imap_fetch_overview($inbox, $num, 0);
- $message = imap_fetchbody($inbox, $num, 1);
- // $message = imap_body($inbox, $num);
- /* output the email header information */
- $output.= $overview[0]->seen ? ‘<div>’ : ‘<strong style=”display:block” mce_style=”display:block”>’;
- if (preg_match_all(‘/=/?[^/?]+/?B/?([a-zA-Z0-9/+//=]+)/?=/i’, $overview[0]->subject, $subjects)) {
- $subject = ”;
- foreach ($subjects[0] as $value) {
- $subject .= decodeBase64($value);
- }
- } else {
- $subject = $overview[0]->subject;
- }
- /* if this mail subject contain “Foxmail”, then delete it */
- if (strstr($subject, ‘Foxmail’)) {
- imap_setflag_full($inbox, $num, ‘//Deleted’);
- // imap_delete($inbox, $num, 1);
- }
- $output .= $subject;
- $output.= $overview[0]->seen ? ‘</div>’ : ‘</strong>’;
- $output.= ‘From: ‘ . decodeBase64($overview[0]->from);
- $output.= ‘<br/>Date: ‘ . $overview[0]->date;
- /* output the email body */
- $output.= ‘<div style=”border:1px dashed #000; padding:10px;” mce_style=”border:1px dashed #000; padding:10px;”>’ . $message . ‘</div>’;
- }
- echo $output;
- }
- function decodeBase64($str) {
- if (preg_match(‘/=/?[^/?]+/?B/?[a-zA-Z0-9/+//=]+/?=/i’, $str)) {
- $str = preg_replace(array(‘/=/?[^/?]+/?B/?/i’, ‘//?=/’), array(”, ”), $str);
- $str = base64_decode($str);
- }
- return $str;
- }
- /* close the connection */
- imap_close($inbox);
–Love science, Enjoy life.