A simple (or incomplete) IMAP class and application

Source: Internet
Author: User
Tags imap
? PhpbyAlpha. z05212000classmyimap {var $ username; var $ userpwd; var $ hostname; var $ port0; var $ connection0; whether to connect var $ stateDISCONNECTED; connection status var $ greeting; var $ must_update0; var $ inStream0; Functionopen () {if ($ this-port1 IMAP

// By Alpha. Z
// 05/21/2000

Class myimap
{
Var $ username = "";
Var $ userpwd = "";
Var $ hostname = "";
Var $ port = 0;
Var $ connection = 0; // whether to connect
Var $ state = "DISCONNECTED"; // connection status
Var $ greeting = "";
Var $ must_update = 0;
Var $ inStream = 0;

Function open ()
{
If ($ this-& gt; port = 110)
$ This-> inStream = imap_open ("{$ this-> hostname/pop3: 110} inbox", $ this-> username, $ this-> userpwd );
Else
$ This-> inStream = imap_open ("{$ this-> hostname/imap: 143} inbox", $ this-> username, $ this-> userpwd );

If ($ this-> inStream)
{
Echo "User: $ this-> username mailbox connection successful.
";
Return $ inStream;
}
Else
{
Echo "User: $ this-> username mailbox connection failed.
";
Return 0;
}
}

Function close ()
{
If (imap_close ($ this-> inStream ))
{
Echo "has been disconnected from the server $ this-> hostname. ";
Return 1;
}
Else
{
Echo "failed to disconnect from the server $ this-> hostname. ";
Return 0;
}
}

Function CheckMailbox ()
{
$ Mboxinfo = @ imap_mailboxmsginfo ($ this-> inStream );
// $ Mboxinfo = imap_check ($ this-> inStream );
If ($ mboxinfo)
If ($ mboxinfo-> Nmsgs> 0)
{
Echo "total number of emails in your inbox:". $ mboxinfo-> Nmsgs ."
";
Echo "Unread mails:". $ mboxinfo-> Unread ."
";
// Echo "new mail quantity:". $ mboxinfo-> Rescent ."
";
Echo "total space occupied:". $ mboxinfo-> Size. "bytes
";
Echo "latest Mail Date:". $ mboxinfo-> Date ."
";
}
Else
{
Echo "there is no email in your mailbox.
";
}
Else
{
Echo 'Error: unable to get the inbox information. ';
}

Echo'


























































';$ Sortby = "SORTDATE ";$ Sort_reverse = 1;$ Sorted = imap_sort ($ this-> inStream, $ sortby, $ sort_reverse, SE_UID );For ($ I = 1; $ I <= $ mboxinfo-> Nmsgs; $ I ++){$ MsgHeader = @ imap_header ($ this-> inStream, imap_msgno ($ this-> inStream, $ sorted [$ i-1]);// DateIf (isset ($ msgHeader-> date )){$ Date = $ msgHeader-> date;If (ord ($ date)> 64)$ Date = substr ($ date, 5 );While (strstr ('', $ date )){$ Date = str_replace ('','', $ date );}}If (isset ($ msgHeader-> from [0]){$ From = $ msgHeader-> from [0];If (isset ($ from-> personal )){$ Frm = trim ($ from-> personal );}ElseIf (isset ($ from-> mailbox) & isset ($ from-> host )){$ Frm = $ from-> mailbox. '@'. $ from-> host;}ElseIf (isset ($ msgHeader-> fromaddress ))$ Frm = trim ($ h-> fromaddress );}ElseIf (isset ($ msgHeader-> fromaddress ))$ Frm = trim ($ msgHeader-> fromaddress );If (isset ($ msgHeader-> toaddress ))$ To = trim ($ msgHeader-> toaddress );Else$ To = "unknown "; If (isset ($ msgHeader-> subject ))$ Sub = trim ($ this-> decode_mime_string ($ msgHeader-> subject ));If (isset ($ msgHeader-> Size ))$ Msg_size = ($ msgHeader-> Size> 1024 )? Sprintf ("%. 0f kb", $ msgHeader-> Size/1024): $ msgHeader-> Size;If (strlen ($ frm)> 50)$ Frm = substr ($ frm, 0, 50 ).'...';If (strlen ($ sub)> 50)$ Sub = substr ($ sub, 0, 50 ).'...';Echo' ';Echo' ';Echo' ';}Echo"
'. $ Frm .' Username. '& pwd ='. $ this-> userpwd. '& msg ='. $ I. '">'. $ sub .' '. $ Date .' '. $ Msg_size .'
";
}

Function decode_mime_string ($ string)
{
$ Pos = strpos ($ string, '=? ');
If (! Is_int ($ pos ))
{
Return $ string;
}

$ Preceding = substr ($ string, 0, $ pos); // save any preceding text

$ Search = substr ($ string, $ pos + 2, 75); // the mime header spec says this is the longest a single encoded word can be
$ D1 = strpos ($ search ,'? ');
If (! Is_int ($ d1 ))
{
Return $ string;
}

$ Charset = substr ($ string, $ pos + 2, $ d1 );
$ Search = substr ($ search, $ d1 + 1 );

$ D2 = strpos ($ search ,'? ');
If (! Is_int ($ d2 ))
{
Return $ string;
}

$ Encoding = substr ($ search, 0, $ d2 );
$ Search = substr ($ search, $ d2 + 1 );

$ End = strpos ($ search ,'? = ');
If (! Is_int ($ end ))
{
Return $ string;
}

$ Encoded_text = substr ($ search, 0, $ end );
$ Rest = substr ($ string, (strlen ($ preceding. $ charset. $ encoding. $ encoded_text) + 6 ));

Switch ($ encoding)
{
Case 'Q ':
Case 'Q ':
$ Encoded_text = str_replace ('_', '% 20', $ encoded_text );
$ Encoded_text = str_replace ('=', '%', $ encoded_text );
$ Decoded = urldecode ($ encoded_text );
Break;

Case 'B ':
Case 'B ':
$ Decoded = urldecode (base64_decode ($ encoded_text ));
Break;

Default:
$ Decoded = '=? '. $ Charset .'? '. $ Encoding .'? '. $ Encoded_text .'? = ';
Break;
}

Return $ preceding. $ decoded. decode_mime_string ($ rest );
}


Function display_toaddress ($ user, $ server, $ from)
{
Return is_int (strpos ($ from, $ this-> get_barefrom ($ user, $ server )));
}

Function get_barefrom ($ user, $ server)
{
$ Barefrom = "$ user @ $ real_server ";

Return $ barefrom;
}

Function get_structure ($ msg_num)
{
$ Structure = imap_fetchstructure ($ this-> inStream, $ msg_num );
// Echo gettype ($ structure );
Return $ structure;
}

Function proc_structure ($ msg_part, $ part_no, $ msg_num)
{
If ($ msg_part-> ifdisposition)
{
// See if it has a disposition
// The only thing I know of that this
// Wocould be used for wocould be an attachment
// Lets check anyway
If ($ msg_part-> disposition = "ATTACHMENT ")
{
// If it is an attachment, then we let people download it
// First see if they sent a filename
$ Att_name = "unknown ";
For ($ lcv = 0; $ lcv <count ($ msg_part-> parameters); $ lcv ++)
{
$ Param = $ msg_part-> parameters [$ lcv];

If ($ param-> attribute = "NAME ")
{
$ Att_name = $ param-> value;
Break;
}
}

// You cocould give a link to download the attachment here ....
Echo ''. $ att_name .'
';
$ Fp = fopen (". \ $ att_name", "w + ");
Fputs ($ fp, imap_base64 (imap_fetchbody ($ this-> inStream, $ msg_num, $ part_no )));
Fclose ($ fp );
}
Else
{
// I guess it is used for something besides attachments ????
}
}
Else
{
// Not an attachment, lets see what this part is...
Switch ($ msg_part-> type)
{
Case TYPETEXT:
$ Mime_type = "text ";
Break;
Case TYPEMULTIPART:
$ Mime_type = "multipart ";
// Hey, why not use this function to deal with all the parts
// Of this multipart part :)
For ($ I = 0; $ I <count ($ msg_part-> parts); $ I ++)
{
If ($ part_no! = "")
{
$ Part_no = $ part_no .".";
}
For ($ I = 0; $ I <count ($ msg_part-> parts); $ I ++)
{
$ This-> proc_structure ($ msg_part-> parts [$ I], $ part_no. ($ I + 1), $ msg_num );
}
}
Break;
Case TYPEMESSAGE:
$ Mime_type = "message ";
Break;
Case TYPEAPPLICATION:
$ Mime_type = "application ";
Break;
Case TYPEAUDIO:
$ Mime_type = "audio ";
Break;
Case TYPEIMAGE:
$ Mime_type = "image ";
Break;
Case TYPEVIDEO:
$ Mime_type = "video ";
Break;
Case TYPEMODEL:
$ Mime_type = "model ";
Break;
Default:
$ Mime_type = "unknown ";
// Hmmm ....
}

$ Full_mime_type = $ mime_type. "/". $ msg_part-> subtype;
// Echo $ full_mime_type .'';

// Decide what you what to do with this part
// If you want to show it, figure out the encoding and echo away
Switch ($ msg_part-> encoding)
{
Case ENCBASE64:
// Use imap_base64 to decode
$ Fp = fopen (". \ $ att_name", "w + ");
Fputs ($ fp, imap_base64 (imap_fetchbody ($ this-> inStream, $ msg_num, $ part_no )));
Fclose ($ fp );
Break;
Case ENCQUOTEDPRINTABLE:
// Use imap_qprint to decode
// Echo ereg_replace ("\ n ","
", Imap_qprint (imap_fetchbody ($ this-> inStream, $ msg_num, $ part_no )));
Echo'
'.imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)).'
';
Break;
Case ENCOTHER:
// Not sure if this needs decoding at all
Break;
Default:
}
}
}


};

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.