📄 imap.class.inc
字号:
* @param String $header The plain text header to search through * @access private * @return string The value of the fieldname if found. */ function get_header_value($fieldname, $header) { $resu = ''; $header = eregi_replace("\t", " ", $header); $results = array (); if (eregi("$fieldname (.*)", $header, $results)) { $fieldval = $results[1]; for ($b = 0; $b <= strlen($fieldval); $b ++) { $curr = substr($fieldval, $b, 1); $next = substr($fieldval, $b +1, 1); if ($curr == "\n" && $next != " ") { break; } if ($curr == "\t") { $curr = " "; } if ($curr == "\n") { $curr = ""; } $resu .= $curr; } } $resu = eregi_replace("\([^\)]*\)", "", $resu); return trim($resu); } /** * Delete messages from the IMAP server * * @param Array $messages An array of message UID's * @access public * @return void */ function delete($messages) { for ($i = 0; $i < count($messages); $i ++) { @ imap_delete($this->conn, $messages[$i], FT_UID); } @ imap_expunge($this->conn); } /** * Return a message part * * @param int $uid The message UID * @param float $part_no The message part identifier * @param String $transfer The transfer-encoding * @param String $part_charset The character set of the part if applicable * @access public * @return string Message part data */ function view_part($uid, $part_no, $transfer, $part_charset = '') { global $GO_CONFIG, $charset; $str = imap_fetchbody($this->conn, $uid, $part_no, FT_UID); if ($transfer == 'BASE64') { $str = imap_base64($str); }elseif ($transfer == 'QUOTED-PRINTABLE') { $str = quoted_printable_decode($str); /* * work around for some characters. * They seem only to occur when transfer-encoding is quoted-printable. * * $pos = strpos($str,'20.000')-2; * echo $str[$pos].' = '.ord($str[$pos]); */ $str = str_replace(chr(128), "€", $str); $str = str_replace(chr(133), ".", $str); $str = str_replace(chr(146), "'", $str); $str = str_replace(chr(147), '"', $str); $str = str_replace(chr(148), '"', $str); } if (function_exists('iconv') && $part_charset != '' && $part_charset != $charset) { //echo 'Charset:'.$part_charset.' -- > '.$charset.' '.$transfer.'<br>'; if($converted = @iconv($part_charset, $charset.'//IGNORE', $str)) { return str_replace($part_charset, $charset, $converted); } } return ($str); } function f($name) { $value = isset ($this->message[$name]) ? $this->message[$name] : false; return $value; } /** * Get the delimiter that is used to delimit Mailbox names * * @access public * @return mixed The delimiter or false on failure */ function get_mailbox_delimiter() { $list = imap_getmailboxes($this->conn, "{".$this->connectstring."}", '%'); if (is_array($list)) { $folder = array_shift($list); if (strlen($folder->delimiter) > 0) { return $folder->delimiter; } } return false; } /** * Check if the given mailbox root is valid and return it with the correct delimiter * * @param $mbroot The Mailbox root. (eg. INBOX/) * @access public * @return mixed Mailbox root with delimiter or false on failure */ function check_mbroot($mbroot) { $mbroot = trim($mbroot); $list = imap_getmailboxes($this->conn, "{".$this->connectstring."}", '%'); if (is_array($list)) { while ($folder = array_shift($list)) { if (!isset ($delimiter) && strlen($folder->delimiter) > 0) { $delimiter = $folder->delimiter; if (substr($mbroot, -1) == $delimiter) { $mbroot = substr($mbroot, 0, -1); } } if (str_replace("{".$this->connectstring."}", "", $folder->name) == $mbroot) { return $mbroot.$delimiter; } } } return false; } /** * Return all mailboxes in an array with name, delimiter and attributes * * @param String $mailbox_root The mailbox root * @param bool $name_only Return only the mailbox names in the result * @access public * @return array The mailboxes */ function get_mailboxes($mailbox_root = '', $name_only = false) { //echo $mailbox_root.' -- > '; //echo '>"'.$mailbox_root.'" -> "'.$this->utf7_encode($mailbox_root).'"<hr />'; $this->mailboxes = array (); $list = imap_getmailboxes($this->conn, "{".$this->connectstring."}", $mailbox_root.'%'); if (is_array($list)) { foreach ($list as $value) { if (substr($value->name, -1) != $value->delimiter && strlen($value->delimiter) > 0) { if ($name_only) { $this->mailboxes[] = str_replace("{".$this->connectstring."}", "", $value->name); } else { $mailbox['name'] = str_replace("{".$this->connectstring."}", "", $value->name); $mailbox['delimiter'] = $value->delimiter; $mailbox['attributes'] = $value->attributes; $this->mailboxes[] = $mailbox; if (!($mailbox['attributes'] & LATT_NOINFERIORS)) { $this->mailboxes = array_merge($this->mailboxes, $this->get_mailboxes($mailbox['name'].$mailbox['delimiter'])); } } } } } return $this->mailboxes; } /** * Return all subscribed mailboxes in an array with name, delimiter and attributes * * @param String $mailbox_root The mailbox root * @param bool $name_only Return only the mailbox names in the result * @access public * @return array The subscribed mailboxes */ function get_subscribed($mailbox_root = '', $name_only = false) { $this->mailboxes = array (); $list = imap_getsubscribed($this->conn, "{".$this->connectstring."}", $mailbox_root.'%'); if (is_array($list)) { foreach ($list as $value) { if (substr($value->name, -1) != $value->delimiter && strlen($value->delimiter) > 0) { $mailbox['name'] = str_replace("{".$this->connectstring."}", "",$value->name); $mailbox['delimiter'] = $value->delimiter; $mailbox['attributes'] = $value->attributes; if ($name_only) { $this->mailboxes[] = $mailbox['name']; } else { $this->mailboxes[] = $mailbox; } if (!($mailbox['attributes'] & LATT_NOINFERIORS)) { $this->mailboxes = array_merge($this->mailboxes, $this->get_subscribed($mailbox['name'].$mailbox['delimiter'], $name_only)); } } } } return $this->mailboxes; } /** * Check if a mailbox is subscribed * * @param String $name The name of the mailbox * @param String $mailbox_root The mailbox root * @access public * @return bool */ function is_subscribed($name, $mailbox_root) { $this->get_subscribed($mailbox_root); for ($i = 0; $i < count($this->mailboxes); $i ++) { if ($this->mailboxes[$i]['name'] == $name) { return true; } } return false; } /** * Subscribe a mailbox * * @param String $name The name of the mailbox * @access public * @return bool True on success */ function subscribe($name) { return imap_subscribe($this->conn, "{".$this->connectstring."}".$name); } /** * Unsubscribe a mailbox * * @param String $name The name of the mailbox * @access public * @return bool True on success */ function unsubscribe($name) { return imap_unsubscribe($this->conn, "{".$this->connectstring."}".$name); } /** * Delete a mailbox * * @param String $name The name of the mailbox * @param String $mailbox_root The mailbox root * @access public * @return bool True on success */ function delete_folder($name, $mailbox_root) { if ($this->is_subscribed($name, $mailbox_root)) { if ($this->unsubscribe($name)) { return imap_deletemailbox($this->conn, "{".$this->connectstring."}".$name); } return false; } else { return imap_deletemailbox($this->conn, "{".$this->connectstring."}".$name); } } /** * Create a mailbox * * @param String $name The name of the mailbox * @access public * @return bool True on success */ function create_folder($name, $delimiter='.') { //echo imap_utf7_encode(utf8_decode($name)).' -> '.$this->utf7_encode($name); if (imap_createmailbox($this->conn, "{".$this->connectstring."}".$name)) { return $this->subscribe($name); } } /** * Rename a mailbox * * @param String $old_name The current name of the mailbox * @param String $new_name The new name of the mailbox * @access public * @return bool True on success */ function rename_folder($old_name, $new_name) { $children = $this->get_mailboxes($old_name); if(imap_renamemailbox($this->conn, "{".$this->connectstring."}".$old_name, "{".$this->connectstring."}".$new_name)) { foreach($children as $old_child) { $old_child = $old_child['name']; $pos = strpos($old_child, $old_name); $new_child = substr_replace($old_child, $new_name, $pos, strlen($old_name)); //echo 'Renaming: '.$old_child.' to: '.$new_child.' <br />'; if(!$this->unsubscribe($old_child)) { return false; }elseif(!$this->subscribe($new_child)) { if(imap_renamemailbox($this->conn, "{".$this->connectstring."}".$old_child, "{".$this->connectstring."}".$new_child)) { if(!$this->subscribe($new_child)) { return false; } } } } } return true; } /** * Move messages to another mailbox * * @param String $folder The mailbox where the messages need to go * @param Array $messages An array of message UID's to move * @access public * @return bool True on success */ function move($folder, $messages) { $messageset = implode(",", $messages); if (imap_mail_move($this->conn, $messageset, $folder, CP_UID)) { imap_expunge($this->conn); return true; } return false; } /** * Append a message to a maibox * * @param String $mailbox The mailbox where the message needs to go * @param String $body The message body * @param Int $flags The message flags (Unseen, Replied, flagged) * @access public * @return bool True on success */ function append_message($mailbox, $body, $flags = "") { if (@ imap_append($this->conn, "{".$this->connectstring."}".$mailbox, $body, $flags)) { return true; } else { return false; } } /** * Set message flags * * @param String $mailbox The mailbox where the message is * @param String $uid_array An array of message UID's * @param Int $flags The message flags (Unseen, Replied, flagged) * @param String $action If action is reset the given flags will be cleared * @access public * @return bool True on success */ function set_message_flag($mailbox = "INBOX", $uid_array, $flags, $action = "") { if ($mailbox == $this->mailbox || $this->reopen($mailbox)) { $msgno_set = implode(",", $uid_array); if ($action == "reset") { if (imap_clearflag_full($this->conn, $msgno_set, $flags, ST_UID)) { return true; } else { return false; } } else { if (imap_setflag_full($this->conn, $msgno_set, $flags, ST_UID)) { return true; } else { return false; } } } else { return false; } } /** * Re-open a mailbox * * @param String $mailbox The mailbox to open * @param Int $flags Connection flags see imap_open() PHP docs. * @access public * @return bool True on success */ function reopen($mailbox = "INBOX", $flags = "") { if (imap_reopen($this->conn, "{".$this->connectstring."}".$mailbox, $flags)) { $this->mailbox = $mailbox; return true; } else { return false; } } /** * Get mailbox message info and put it in $this->mailbox_info. * * @access public * @return bool True on success */ function mailbox_info() { $info = imap_mailboxmsginfo($this->conn); if ($info) { $this->mailbox_info = array (); $this->mailbox_info["date"] = $info->Date; $this->mailbox_info["driver"] = $info->Driver; $this->mailbox_info["mailbox"] = $info->Mailbox; $this->mailbox_info["nmsgs"] = $info->Nmsgs; $this->mailbox_info["recent"] = $info->Recent; $this->mailbox_info["size"] = $info->Size; return true; } else { return false; } } }?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -