ERROR = _("MBOX connect:") . ' ' . _("No server specified"); return false; } $this->MBOXSTR = "{".$server.":".$port."/pop3/ssl/novalidate-cert}"; $this->MBOX = imap_open( $this->MBOXSTR, $login, $password ); if (!$this->MBOX) { print_r(imap_errors()); $this->ERROR = imap_errors(); return false; } return true; } function get_count() { /* XXX: Apparently Gmail does some funky backend shit when processing a * user's account. Probably something to do with their schnazzy * filesystem. Basically their pop server isn't competely synced with * whatever the browser sees. As such, for some reason only completely * untouched gmail msgs can be seen. */ $status = imap_status( $this->MBOX, $this->MBOXSTR, SA_ALL ); if ($status) { if ($this->DEBUG) { echo "Messages: " . $status->messages . "
\n"; echo "Recent: " . $status->recent . "
\n"; echo "Unseen: " . $status->unseen . "
\n"; echo "UIDnext: " . $status->uidnext . "
\n"; echo "UIDvalidity:" . $status->uidvalidity . "
\n"; } return $status->unseen; } else { $this->ERROR = imap_last_error(); } return false; } function parse_header($msgNum) { $header = imap_header( $this->MBOX, $msgNum ); $message = array(); $fullsubject = ''; // most 'froms' are just aliased names; we want an actual address $from = $header->from[0]->mailbox."@".$header->from[0]->host; $message['fromaddress'] = $from; $message['toaddress'] = $header->toaddress; // Subject may have different charset, and mixes of charsets too (argh) $elements = imap_mime_header_decode($header->subject); for( $i =0; $i < count($elements); $i++) { $charset = $elements[$i]->charset; $text = $elements[$i]->text; if ($this->DEBUG) { echo "charset: " . $charset . "
"; echo "text: " . $text . "
"; } if(strcasecmp($charset,"utf-8") != 0) { if (strcasecmp($charset,"default") == 0) $charset = "US-ASCII"; $text = iconv($charset,"UTF-8",$text); if ($this->DEBUG) echo "
converted subject to UTF-8
"; } $fullsubject .= $text; } $message['subject'] = $fullsubject; $message['date'] = $header->date; if ($this->DEBUG) { echo "
PARSE HEADER produced:

"; var_dump( $header ); echo "

"; echo "From: ".$message['fromaddress']."
"; echo "To: ".$message['toaddress']."
"; echo "Date: ".$message['date']."
"; echo "Subject: ".$message['subject']."
"; echo "


"; } return $message; } function parse_content($msgNum) { $retval = imap_fetchstructure( $this->MBOX, $msgNum ); $message = array(); // find how many parts object has $numparts = count( $retval->parts ); if ($this->DEBUG) { echo "FETCHSTRUCTURE (".$numparts." part(s)):
"; var_dump( $retval ); } $i = 0; foreach ($retval->parts as $part) { $i++; $charset = ''; if ($this->DEBUG) { echo "
Processing part ".$i."...

\n"; echo "part dump:
"; var_dump($part); echo "
"; echo "Of type: ".$part->type."
"; echo "Of subtype: ".$part->subtype."
"; echo "Of encoding: ".$part->encoding."
"; } $message['part'.$i]['type'] = $part->type; $message['part'.$i]['subtype'] = $part->subtype; $message['part'.$i]['encoding'] = $part->encoding; if ($part->ifdparameters) { if ($part->dparameters[0]->attribute == "FILENAME") { $message['part'.$i]['filename']=$part->dparameters[0]->value; } } elseif ($part->ifparameters) { if ($part->parameters[0]->attribute == "CHARSET") { $message['part'.$i]['charset'] = $part->parameters[0]->value; } } // XXX: not sure how robust id calculation is ... $message['part'.$i]['body'] = imap_fetchbody($this->MBOX, $msgNum, $i); } $message['parts'] = $i; return $message; } // helper func to better understand imap_fetchbody id's /* function print_structure($msgNum) { $msg =& new message_components($this->MBOX); $msg->fetch_structure($msgNum); echo '
';    
		 var_dump($msg->pid[$msgNum]);
		 echo '
'; } */ function fix_coding($type, $body) { if ($this->DEBUG) { echo "fixing coding for type " . $type . "
"; } switch($type) { case 0: return $body; // do nothing? break; case 1: return imap_8bit($body); break; case 2: return imap_binary($body); break; case 3: return base64_decode($body); break; case 4: // return utf8_decode(imap_utf8($body)); <-- use this one instead?? // return quoted_printable($body); break; case 5: return $body; break; } } function delete_msg($msgNum = "") { /* per the impp_ php docs: POP3 mailboxes do not have their message flags * saved between connections, so imap_expunge() must be called during the * same connection in order for messages marked for deletion to actually be * purged. */ return imap_delete( $this->MBOX, $msgNum ); } function reset() { /* Resets status of remote server. Includes resetting status of ALL * messages to not be deleted. Automatically closes connection to * server. */ // XXX: how to define range for all msgs? imap_clearflag_full( $this->MBOX, "1,10", "\\Deleted" ); return $this->quit(); } function quit() { /* Close connection to mail server, deleting any messages as marked. * XXX: As per note in get_count(), Gmail's FS funkifies things and * deletion is currently not working ... */ $retval = imap_close( $this->MBOX, CL_EXPUNGE ); if ($retval) { return true; } else { echo "quitting failed: " . imap_errors() . "
\n"; return false; } } } /* class mbox */ ?>