Mailbox.class.php (5523B)
1 <?php 2 3 class Mailbox 4 { 5 public $debug = false; 6 public $config = array(); 7 8 private $text; 9 private $html; 10 private $filenames; 11 12 public function pull() 13 { 14 15 $entrys = array(); 16 17 /* connect to gmail */ 18 $hostname = $this->config['host']; 19 $username = $this->config['username']; 20 $password = $this->config['password']; 21 22 /* try to connect */ 23 $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to IMAP server: ' . imap_last_error()); 24 25 /* grab emails */ 26 $emails = imap_search($inbox,'ALL UNDELETED UNSEEN'); 27 28 print_r($emails); 29 30 /* if emails are returned, cycle through each... */ 31 if($emails) { 32 33 /* put the newest emails on top */ 34 rsort($emails); 35 36 /* for every email... */ 37 foreach($emails as $email_number) { 38 39 /* get information specific to this email */ 40 // $overview = imap_fetch_overview($inbox,$email_number,0); 41 $headers = imap_headerinfo($inbox,$email_number); 42 $structure = imap_fetchstructure($inbox,$email_number); 43 // $message = imap_fetchbody($inbox,$email_number); 44 45 // echo "\nOverview:"; 46 // print_r($overview); 47 if ( $this->debug ) { echo '<pre>'; print_r($headers); echo '</pre>'; } 48 49 // Initalize 50 $this->filenames = array(); 51 $this->text = ''; 52 $this->html = ''; 53 $subject = iconv_mime_decode($headers->subject,0,'UTF-8'); 54 55 $s = imap_fetchstructure($inbox,$email_number); 56 57 if (!$s->parts) // simple 58 $this->getpart($inbox,$email_number,$s,0); // pass 0 as part-number 59 else { // multipart: cycle through each part 60 foreach ($s->parts as $partno0=>$p) 61 $this->getpart($inbox,$email_number,$p,$partno0+1); 62 } 63 64 // print_r($message); 65 /* output the email header information */ 66 // $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 67 // $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 68 // $output.= '<span class="from">'.$overview[0]->from.'</span>'; 69 // $output.= '<span class="date">on '.$overview[0]->date.'</span>'; 70 // $output.= '</div>'; 71 72 /* output the email body */ 73 // $output.= '<div class="body">'.$message.'</div>'; 74 75 if ( $this->debug ) echo "\n\nBetreff: ".$subject; 76 if ( $this->debug ) echo "\n\nText: "; 77 if ( $this->debug ) print_r($this->text); 78 if ( $this->debug ) echo "\n\nAnlagen: "; 79 if ( $this->debug ) print_r($this->filenames); 80 81 $entrys[] = array( 82 'filenames'=> $this->filenames, 83 'keywords' => array(), 84 'timestamp' => strtotime($headers->date), 85 'subject' => $subject, 86 'text' => $this->text 87 ); 88 89 // Aufräumen: 90 // - Mail als gelesen markieren und in das Archiv verschieben. 91 if ( $this->config['dry'] ) 92 ; 93 else 94 { 95 imap_setflag_full($inbox,$email_number,'\\SEEN',0); 96 97 if (isset($this->config['archive_folder'])) 98 { 99 imap_mail_move($inbox,$email_number,$this->config['archive_folder']) or die("IMAP: Move did not suceed: "+imap_last_error() ); 100 101 imap_expunge($inbox); 102 } 103 } 104 } 105 } 106 107 /* close the connection */ 108 imap_close($inbox); 109 110 return $entrys; 111 } 112 113 114 115 private function getpart($mbox,$mid,$p,$partno) 116 { 117 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple 118 119 // DECODE DATA 120 $data = ($partno) ? 121 imap_fetchbody($mbox,$mid,$partno): // multipart 122 imap_body($mbox,$mid); // simple 123 124 // Any part may be encoded, even plain text messages, so check everything. 125 if ($p->encoding==4) 126 $data = quoted_printable_decode($data); 127 elseif ($p->encoding==3) 128 $data = base64_decode($data); 129 130 // PARAMETERS 131 // get all parameters, like charset, filenames of attachments, etc. 132 $params = array(); 133 if (@$p->parameters) 134 foreach ($p->parameters as $x) 135 $params[strtolower($x->attribute)] = $x->value; 136 if (@$p->dparameters) 137 foreach ($p->dparameters as $x) 138 $params[strtolower($x->attribute)] = $x->value; 139 140 // ATTACHMENT 141 // Any part with a filename is an attachment, 142 // so an attached text file (type 0) is not mistaken as the message. 143 if (@$params['filename'] || @$params['name']) { 144 // filename may be given as 'Filename' or 'Name' or both 145 $filename = ($params['filename'])? $params['filename'] : $params['name']; 146 // filename may be encoded, so see imap_mime_header_decode() 147 $fname = tempnam(null,'blog-file-'); 148 $file = fopen($fname,'w'); 149 fwrite($file,$data); 150 fclose($file); 151 chmod($fname,0644); 152 153 $this->filenames[] = array('filename'=>$fname,'name'=>$filename); 154 } 155 156 // TEXT 157 if ($p->type==0 && $data) { 158 $charset = $params['charset']; // assume all parts are same charset 159 $data = iconv($charset,'UTF-8//TRANSLIT',$data); 160 161 // Messages may be split in different parts because of inline attachments, 162 // so append parts together with blank row. 163 if (strtolower($p->subtype)=='plain') 164 $this->text.= trim($data) ."\n\n"; 165 else 166 $this->html.= $data ."<br><br>"; 167 } 168 169 // EMBEDDED MESSAGE 170 // Many bounce notifications embed the original message as type 2, 171 // but AOL uses type 1 (multipart), which is not handled here. 172 // There are no PHP functions to parse embedded messages, 173 // so this just appends the raw source to the main message. 174 elseif ($p->type==2 && $data) { 175 $this->text.= $data."\n\n"; 176 } 177 178 // SUBPART RECURSION 179 if (@$p->parts) { 180 foreach ($p->parts as $partno0=>$p2) 181 $this->getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc. 182 } 183 } 184 185 } 186 187 ?>