Mailbox.class.php (2850B)
1 <?php 2 3 class Mailbox 4 { 5 public $debug = false; 6 public $config = array(); 7 8 private $text; 9 private $html; 10 11 public function pull() 12 { 13 14 $entrys = array(); 15 16 /* connect to gmail */ 17 $hostname = $this->config['host']; 18 $username = $this->config['username']; 19 $password = $this->config['password']; 20 21 /* try to connect */ 22 $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to IMAP server: ' . imap_last_error()); 23 24 /* grab emails */ 25 $emails = imap_search($inbox,'ALL UNDELETED UNSEEN'); 26 27 print_r($emails); 28 29 /* if emails are returned, cycle through each... */ 30 if($emails) { 31 32 /* put the newest emails on top */ 33 rsort($emails); 34 35 /* for every email... */ 36 foreach($emails as $email_number) { 37 38 /* get information specific to this email */ 39 // $overview = imap_fetch_overview($inbox,$email_number,0); 40 $headers = imap_headerinfo($inbox,$email_number); 41 $structure = imap_fetchstructure($inbox,$email_number); 42 // $message = imap_fetchbody($inbox,$email_number); 43 44 // echo "\nOverview:"; 45 // print_r($overview); 46 if ( $this->debug ) { echo '<pre>'; print_r($headers); echo '</pre>'; } 47 48 // Initalize 49 $this->filenames = array(); 50 $this->text = ''; 51 $this->html = ''; 52 $subject = iconv_mime_decode($headers->subject,0,'UTF-8'); 53 54 $s = imap_fetchstructure($inbox,$email_number); 55 56 // Hier gibt es keinen MIME-Messages, es reicht also der Body. 57 $this->text = imap_body($inbox,$email_number); 58 59 if ( $this->debug ) echo "\n\nBetreff: ".$subject; 60 if ( $this->debug ) echo "\n\nText: "; 61 if ( $this->debug ) print_r($this->text); 62 63 64 $header_string = imap_fetchheader($inbox, $email_number); 65 preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m',$header_string, $matches); 66 $allHeaders = array_combine($matches[1], $matches[2]); 67 68 if ( $this->debug ) echo "\n\nAlle Mailheader: "; 69 if ( $this->debug ) echo "<pre>$header_string</pre>"; 70 if ( $this->debug ) {echo "<pre>"; print_r($allHeaders); echo "</pre>";} 71 72 $entrys[] = array( 73 'timestamp' => strtotime($headers->date), 74 'subject' => $subject, 75 'text' => $this->text, 76 'name' => $allHeaders['X-Name'], 77 'pageid' => $allHeaders['X-Page-Id'] 78 ); 79 80 // Aufräumen: 81 // - Mail als gelesen markieren und in das Archiv verschieben. 82 if ( $this->config['dry'] ) 83 ; 84 else 85 { 86 imap_setflag_full($inbox,$email_number,'\\SEEN',0); 87 88 if (isset($this->config['archive_folder'])) 89 { 90 imap_mail_move($inbox,$email_number,$this->config['archive_folder']) or die("IMAP: Move did not suceed: "+imap_last_error() ); 91 92 imap_expunge($inbox); 93 } 94 } 95 } 96 } 97 98 /* close the connection */ 99 imap_close($inbox); 100 101 return $entrys; 102 } 103 104 } 105 106 ?>