<?php Class Receivemail { var $server = '; var $username = '; var $password = ';
var $marubox = ';
var $email = ';
function Receivemail ($username, $password, $EmailAddress, $mailserver = ' localhost ', $servertype = ' Pop ', $port = ' 110 ', $ SSL = False)//constructure { if ($servertype = = ' IMAP ') { if ($port = = ') $port = ' 143 '; $strConnect = ' {'. $mailserver. ': ' $port. '}inbox '; } Else { $strConnect = ' {'. $mailserver. ': ' $port. '/pop3 '. ($ssl? "/ssl": ""). '} INBOX '; } $this->server = $strConnect; $this->username = $username; $this->password = $password; $this->email = $EmailAddress; }
function connect ()//connect to the Mail Box { $this->marubox= @imap_open ($this->server, $this->username, $this->password); if (! $this->marubox) { echo "error:connecting to mail Server"; Exit } }
function Listmessages ($page = 1, $per _page = =, $sort = null) { $limit = ($per _page * $page); $start = ($limit-$per _page) + 1; $start = ($start < 1)? 1: $start; $limit = (($limit-$start)!= ($per _page-1))? ($start + ($per _page-1)): $limit; $info = Imap_check ($this->marubox); $limit = ($info->nmsgs < $limit)? $info->nmsgs: $limit;
if (true = = Is_array ($sort)) { $sorting = Array ( ' Direction ' => Array (' ASC ' => 0, ' desc ' => 1), ' By ' => Array (' Date ' => sortdate, ' arrival ' => Sortarrival, ' From ' => sortfrom, ' subject ' => sortsubject, ' size ' => sortsize)); $by = (true = = Is_int ($by = $sorting [' by '] [$sort [0]])? $by: $sorting [' by '] [' date ']; $direction = (true = = Is_int ($direction = $sorting [' direction '] [$sort [1]])? $direction: $sorting [' direction '] [' desc ']; $sorted = Imap_sort ($this->marubox, $by, $direction); $msgs = Array_chunk ($sorted, $per _page); $msgs = $msgs [$page-1]; } Else { $MSGS = Range ($start, $limit); Just to keep it consistent } $result = Imap_fetch_overview ($this->marubox, implode ($msgs, ', '), 0); if (false = = = Is_array ($result)) return false;
foreach ($result as $k => $r) { $result [$k]->subject = $this->_imap_utf8 ($r->subject); $result [$k]->from = $this->_imap_utf8 ($r->from); $result [$k]->to = $this->_imap_utf8 ($r->to); $result [$k]->body = $this->getbody ($r->msgno); } sorting! if (true = = Is_array ($sorted)) { $tmp _result = Array (); foreach ($result as $r) { $tmp _result[$r->msgno] = $r; }
$result = Array (); foreach ($msgs as $msgno) { $result [] = $tmp _result[$msgno]; } }
$return = Array (' res ' => $result, ' Start ' => $start, ' Limit ' => $limit, ' Sorting ' => array (' by ' => $sort [0], ' direction ' => $sort [1]), ' Total ' => imap_num_msg ($this->marubox)); $return [' pages '] = ceil ($return [' total ']/$per _page); return $return; }
function Getheaders ($mid)//Get Header Info { if (! $this->marubox) return false; $mail _header=imap_header ($this->marubox, $mid); $sender = $mail _header->from[0]; $sender _replyto= $mail _header->reply_to[0]; if (Strtolower ($sender->mailbox)!= ' Mailer-daemon ' && strtolower ($sender->mailbox)!= ' postmaster ') { $mail _details=array ( ' From ' =>strtolower ($sender->mailbox). ' @ '. $sender->host, ' FromName ' => $sender->personal, ' Tooth ' =>strtolower ($sender _replyto->mailbox). ' @ '. $sender _replyto->host, ' Tonameoth ' => $sender _replyto->personal, ' Subject ' => $mail _header->subject, ' To ' =>strtolower ($mail _header->toaddress) ); } return $mail _details; }
Function Get_mime_type (& $structure)//get MIME type Internal Private use { $primary _mime_type = Array ("TEXT", "MULTIPART", "message", "Application", "AUDIO", "IMAGE", "video", "Other");
if ($structure->subtype) { return $primary _mime_type[(int) $structure->type]. '/' . $structure->subtype; } return "Text/plain"; }
function Get_part ($stream, $msg _number, $mime _type, $structure = False, $part _number = False)//get part of message intern Al Private Use { if (! $structure) { $structure = Imap_fetchstructure ($stream, $msg _number); } if ($structure) { if ($mime _type = = $this->get_mime_type ($structure)) { if (! $part _number) { $part _number = "1"; } $text = Imap_fetchbody ($stream, $msg _number, $part _number); if ($structure->encoding = = 3) { Return imap_base64 ($text); } else if ($structure->encoding = 4) { Return Imap_qprint ($text); } Else { return $text; } } if ($structure->type = 1)/* multipart * * { while (list ($index, $sub _structure) = each ($structure->parts)) { if ($part _number) { $prefix = $part _number. '.'; } $data = $this->get_part ($stream, $msg _number, $mime _type, $sub _structure, $prefix. ($index + 1)); if ($data) { return $data; } } } } return false; }
function Gettotalmails ()//get total number out Unread Email in Mailbox { if (! $this->marubox) return false; $headers =imap_headers ($this->marubox); return count ($headers); } function Getattach ($mid, $path)//Get atteced File from Mail { if (! $this->marubox) { return false; } $struckture = Imap_fetchstructure ($this->marubox, $mid); $ar = ""; if ($struckture->parts) { foreach ($struckture->parts as $key => $value) { $enc = $struckture->parts[$key]->encoding; if ($struckture->parts[$key]->ifdparameters) { $name = $struckture->parts[$key]->dparameters[0]->value; $message = Imap_fetchbody ($this->marubox, $mid, $key + 1); Switch ($ENC) { Case 0: $message = Imap_8bit ($message); Break Case 1: $message = Imap_8bit ($message); Break Case 2: $message = Imap_binary ($message); Break Case 3: $message = Imap_base64 ($message); Break Case 4: $message = Quoted_printable_decode ($message); Break Case 5: $message = $message; Break } $FP =fopen ($path. $name, "w"); Fwrite ($fp, $message); Fclose ($FP); $ar = $ar. $name. ","; } Support for embedded attachments starts here if ($struckture->parts[$key]->parts) { foreach ($struckture->parts[$key]->parts as $keyb => $valueb) { $enc = $struckture->parts[$key]->parts[$keyb]->encoding; if ($struckture->parts[$key]->parts[$keyb]->ifdparameters) { $name = $struckture->parts[$key]->parts[$keyb]->dparameters[0]->value; $partnro = ($key + 1). "." ($keyb + 1); $message = Imap_fetchbody ($this->marubox, $mid, $partnro); Switch ($ENC) { Case 0: $message = Imap_8bit ($message); Break Case 1: $message = Imap_8bit ($message); Break Case 2: $message = Imap_binary ($message); Break Case 3: $message = Imap_base64 ($message); Break Case 4: $message = Quoted_printable_decode ($message); Break Case 5: $message = $message; Break } $FP =fopen ($path. $name, "w"); Fwrite ($fp, $message); Fclose ($FP); $ar = $ar. $name. ","; } } } } } $ar =substr ($ar, 0, (strlen ($ar)-1)); return $ar; }
function GetBody ($mid)//Get message body { if (! $this->marubox) { return false; } $body = $this->get_part ($this->marubox, $mid, "text/html"); if ($body = = "") { $body = $this->get_part ($this->marubox, $mid, "Text/plain"); } if ($body = = "") { Return ""; } return $this->_iconv_utf8 ($body); }
function Deletemails ($mid)//Delete that Mail { if (! $this->marubox) return false;
Imap_delete ($this->marubox, $mid); }
function Close_mailbox ()//close Mail Box { if (! $this->marubox) return false; Imap_close ($this->marubox,cl_expunge); }
function _imap_utf8 ($text) { if (preg_match) ('/=? ( [a-za-z0-9-]+]? (. *) =/i ', $text, $match)) { $text = Imap_utf8 ($text); if (Strtolower substr ($match [1], 0, 2)) = = ' GB ') { $text = Iconv (' GBK ', ' utf-8 ', $text); } return $text; } return $this->_iconv_utf8 ($text); }
function _iconv_utf8 ($text) { $s 1 = iconv (' GBK ', ' utf-8 ', $text); $s 0 = iconv (' utf-8 ', ' GBK ', $s 1); if ($s 0 = $text) { return $s 1; } Else { return $text; } } } |