Email receiving Class

Source: Internet
Author: User
Tags imap
I modified a bit based on the code found on the Internet. All codes are converted to UTF-8 for saving. Support embedded attachments without source code and Demonstration: source code? Php *** NOTICEOFLICENSE *** THISSOURCEFILEISPARTOFEVEBITSPRIVATEPROJECT. ** DONOTUSETHISFILEINOTHERPLACE

I modified a bit based on the code found on the Internet. All codes are converted to UTF-8 for saving. Support embedded attachments without source code and Demonstration: source code? Php/*** notice of license ** this source file is part of evebit's private project. ** DO NOT USE THIS FILE IN OTHER PLACE

I modified a bit based on the code found on the Internet.
All codes are converted to UTF-8 for saving.
Support for Embedded attachments <无>

Source code and demo:Source code

  MailConnect ($ host, $ port, $ user, $ pass, 'inbox', $ ssl); * $ mail-> mail_list ('5, 9: 20 '); * ** show the five, and nine to twenty mail. ** $ mail-> mail_list ('5, '); *** @ docinfo ** @ package Application * @ author Chen Qiao * @ version $ Id: Email. PHP 175 09: 52: 16Z chen. qiao $ */class receiveMail {/*** @ var resource $ _ connect */private $ _ connect; /*** @ var object $ _ mailInfo */private $ _ m AilInfo;/*** @ var int $ _ total_count */private $ _ total_count; /*** @ var array $ _ total_count * // *** _ construct of the class */public function _ construct () {}/*** Open an IMAP stream to a mailbox *** @ param string $ host * @ param string $ port * @ param string $ user * @ param string $ pass *@ param string $ folder * @ param string $ ssl * @ param string $ pop * @ return resource | bool */public funct Ion connect ($ host, $ port, $ user, $ pass, $ folder = "INBOX", $ ssl, $ pop = false) {if ($ pop) {$ ssl = $ pop. '/'. $ ssl. '/novalidate-cert/notls';} $ this-> _ connect = imap_open ("{". "$ host: $ port/$ ssl ". "} $ folder", $ user, $ pass); if (! $ This-> _ connect) {// Evebit_Application: getSession ()-> addError ('cannot connect :'. imap_last_error (); return false;} return $ this-> _ connect ;} /*** Get information about the current mailbox *** @ return object | bool */public function mailInfo () {$ this-> _ mailInfo = imap_mailboxmsginfo ($ this-> _ connection); if (! $ This-> _ mailInfo) {echo "get mailInfo failed :". imap_last_error (); return false;} return $ this-> _ mailInfo ;} /*** Read an overview of the information in the headers of the given message ** @ param string $ msg_range * @ return array */public function mail_list ($ msg_range = '') {if ($ msg_range) {$ range = $ msg_range;} else {$ this-> mail_total_count (); $ range = "1 :". $ this-> _ total_count;} $ overview = Imap_fetch_overview ($ this-> _ connect, $ range); foreach ($ overview as $ val) {$ mail_list [$ val-> msgno] = $ val-> message_id ;} return $ mail_list;}/*** get the total count of the current mailbox *** @ return int */public function mail_total_count () {$ check = imap_check ($ this-> _ connect); $ this-> _ total_count = $ check-> Nmsgs; return $ this-> _ total_count ;} /*** Read the header of the message *** @ param String $ msg_count * @ return array */public function mail_header ($ msg_count) {$ mail_header = array (); $ header = imap_header ($ this-> _ connect, $ msg_count ); if (strtolower ($ sender-> mailbox )! = 'Mailer-daemon' & strtolower ($ sender-> mailbox )! = 'Postmaster') {$ mail_header ['name'] = $ this-> mail_decode ($ header-> subject ); $ mail_header ['mid '] = $ header-> message_id; $ mail_header ['to'] = $ this-> contact_conv ($ header-> ); $ mail_header ['from'] = $ this-> contact_conv ($ header-> from ); $ mail_header ['cc'] = $ this-> contact_conv ($ header-> cc ); $ mail_header ['reply _ to'] = $ this-> contact_conv ($ header-> reply_to); $ create_time = explode (",", $ header-> date ); if (strle N ($ create_time [0])> 6) {$ create_time = $ create_time [0];} else {$ create_time = $ create_time [1];} $ mail_header ['create _ time'] = strtotime ($ create_time); $ subject = $ header-> subject; $ charset = substr ($ subject, stripos ($ subject, "=? ") + 2, stripos ($ subject ,"? ", 3)-2); $ content = $ this-> get_body ($ msg_count); // $ mail_header ['content'] = $ this-> auto_charset ($ content, $ charset, 'utf-8'); $ mail_header ['content'] = $ content;} return $ mail_header ;} /*** decode the subject of chinese *** @ param string $ subject * @ return sting */function mail_decode ($ str) {if (stripos ($ str, 'gbk? B ') {$ arr_temp = explode ("", $ str); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? GBK? B? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp2 = $ tmp2. auto_charset (base64_decode ($ tmp), 'gb2312', 'utf-8');} return $ tmp2;} if (stripos ($ str, 'gbk? Q ') {$ arr_temp = explode ("", $ str); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? GBK? B? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp2 = $ tmp2. auto_charset (base64_decode ($ tmp), 'gb2312', 'utf-8');} return $ tmp2;} if (stripos ($ str, 'utf-8? B ') {$ arr_temp = explode ("", $ str); // dump ($ arr_temp [0]); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? UTF-8? B? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp2 = $ tmp2. base64_decode ($ tmp);} return $ tmp2;} if (stripos ($ str, 'utf-8? Q ') {$ arr_temp = explode ("", $ str); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? UTF-8? Q? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp = str_ireplace ('? ', '', $ Tmp); $ tmp2 = $ tmp2. quoted_printable_decode ($ tmp);} return $ tmp2;} if (stripos ($ str, 'gb2312? B ') {$ arr_temp = explode ("", $ str); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? Gb2312? B? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp2 = $ tmp2. auto_charset (base64_decode ($ tmp), 'gb2312', 'utf-8');} return $ tmp2;} if (stripos ($ str, 'gb2312? Q ') {$ arr_temp = explode ("", $ str); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? Gb2312? Q? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp = str_ireplace ('? ', '', $ Tmp); $ tmp2 = $ tmp2. auto_charset (quoted_printable_decode ($ tmp), 'gb2312', 'utf-8');} return $ tmp2;} if (stripos ($ str, 'gb18030? B ') {$ arr_temp = explode ("", $ str); // dump ($ arr_temp [0]); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? Gb18030? B? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp2 = $ tmp2. auto_charset (base64_decode ($ tmp), 'gb2312', 'utf-8');} return $ tmp2;} if (stripos ($ str, 'gb18030? Q ') {$ arr_temp = explode ("", $ str); for ($ I = 0; $ I <= count ($ arr_temp); $ I ++) {$ tmp = str_ireplace ('=? Gb18030? Q? ', '', $ Arr_temp [$ I]); $ tmp = str_ireplace (' =? ', '', $ Tmp); $ tmp = str_ireplace ('? ', '', $ Tmp); $ tmp2 = $ tmp2. auto_charset (quoted_printable_decode ($ tmp), 'gb18030', 'utf-8');} return $ tmp2;} return $ str;} function auto_charset ($ fContents, $ from, $ to) {$ from = strtoupper ($ from) = 'utf8 '? 'Utf-8': $ from; $ to = strtoupper ($ to) = 'utf8 '? 'Utf-8': $ to; if (strtoupper ($ from) === strtoupper ($ to) | empty ($ fContents) | (is_scalar ($ fContents) &&! Is_string ($ fContents) {// if the encoding is the same or non-string scalar, return $ fContents is not converted;} if (is_string ($ fContents )) {if (function_exists ('mb _ convert_encoding ') {return mb_convert_encoding ($ fContents, $ to, $ from);} elseif (function_exists ('iconv ')) {return iconv ($ from, $ to, $ fContents) ;}else {return $ fContents ;}} elseif (is_array ($ fContents )) {foreach ($ fContents as $ key => $ val) {$ _ key = auto_charset ($ key, $ from, $ ); $ FContents [$ _ key] = auto_charset ($ val, $ from, $ to); if ($ key! =$ _ Key) unset ($ fContents [$ key]);} return $ fContents;} else {return $ fContents ;}} /*** Mark a message for deletion from current mailbox *** @ param string $ msg_count */public function delete ($ msg_count) {imap_delete ($ this-> _ connect, $ msg_count);}/*** get attach of the message ** @ param string $ msg_count * @ param string $ path * @ return array */public function get_attach ($ msg_count, $ path ){ If (! $ This-> _ connect) return false; $ struckture = imap_fetchstructure ($ this-> _ connect, $ msg_count); $ ar = ""; if ($ struckture-> parts) {foreach ($ struckture-> parts as $ key => $ value) {$ enc = $ struckture-> parts [$ key]-> encoding; if ($ struckture-> parts [$ key]-> ifdparameters) {$ name = $ this-> mail_decode ($ struckture-> parts [$ key]-> dparameters [0]-> value ); $ cid = $ struckture-> parts [$ key]-> id; $ cid = substr ($ cid, 1, strlen ($ cid)-2); $ disp Osition = $ struckture-> parts [$ key]-> disposition; $ name = $ cid. "_". $ disposition. "_". $ name; $ message = imap_fetchbody ($ this-> _ connect, $ msg_count, $ key + 1); if ($ enc = 0) $ message = imap_8bit ($ message); if ($ enc = 1) $ message = imap_8bit ($ message); if ($ enc = 2) $ message = imap_binary ($ message); if ($ enc = 3) $ message = imap_base64 ($ message); if ($ enc = 4) $ message = quoted_printable_decode ($ message); if ($ enc = 5) $ message = $ message; $ fp = fopen ($ path. urlencode ($ name), "w"); fwrite ($ fp, $ message); fclose ($ fp); $ ar = $ ar. $ name. ",";} 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 = $ this-> mail_decode ($ struckture-> parts [$ key]-> parts [$ keyb]-> dparameter S [0]-> value); $ id = $ struckture-> parts [$ key]-> parts [$ keyb]-> id; $ disposition = $ struckture-> parts [$ key]-> parts [$ keyb]-> disposition; $ name = $ id. "_". $ disposition. "_". $ name; $ partnro = ($ key + 1 ). ". ". ($ keyb + 1); $ message = imap_fetchbody ($ this-> _ connect, $ msg_count, $ partnro); if ($ enc = 0) $ message = imap_8bit ($ message); if ($ enc = 1) $ message = imap_8bit ($ message); if ($ enc = 2) $ message = imap_binary ($ message); If ($ enc = 3) $ message = imap_base64 ($ message); if ($ enc = 4) $ message = quoted_printable_decode ($ message ); if ($ enc = 5) $ message = $ message; $ fp = fopen ($ path. urlencode ($ name), "w"); fwrite ($ fp, $ message); fclose ($ fp); $ ar = $ ar. $ name. "," ;}}}$ ar = substr ($ ar, 0, (strlen ($ ar)-1); return $ ar ;} /*** download the attach of the mail to localhost *** @ param string $ file_path * @ param string $ message * @ param String $ name */public function down_attach ($ file_path, $ name, $ message) {if (is_dir ($ file_path) {$ file_open = fopen ($ file_path. $ name, "w");} else {mkdir ($ file_path, "0777", true);} fwrite ($ file_open, $ message); fclose ($ file_open );} /*** get the body of the message ** @ param string $ msg_count * @ return string */public function get_body ($ msg_count) {$ body = $ this-> get_part ($ msg_count, "TEXT/HTM L "); if ($ body ='') {$ body = $ this-> get_part ($ msg_count, "TEXT/PLAIN ");} if ($ body = '') {return'';} return $ this-> mail_decode ($ body );} /*** Read the structure of a participant message and fetch a participant * section of the body of the message *** @ param string $ msg_count * @ param string $ mime_type * @ param object $ structure * @ param string $ part_no * @ return string | bool */private functi On get_part ($ msg_count, $ mime_type, $ structure = false, $ part_no = false) {if (! $ Structure) {$ structure = imap_fetchstructure ($ this-> _ connect, $ msg_count);} if ($ structure) {if ($ mime_type ==$ this-> get_mime_type ($ structure) {if (! $ Part_no) {$ part_no = "1" ;}$ from_encoding = $ structure-> parameters [0]-> value; $ text = imap_fetchbody ($ this-> _ connect, $ msg_count, $ part_no); if ($ structure-> encoding = 3) {$ text = imap_base64 ($ text );} else if ($ structure-> encoding = 4) {$ text = imap_qprint ($ text);} $ text = mb_convert_encoding ($ text, 'utf-8 ', $ from_encoding); return $ text;} if ($ structure-> type = 1) {while (list ($ index, $ sub_s Tructure) = each ($ structure-> parts) {if ($ part_no) {$ prefix = $ part_no. '. ';} $ data = $ this-> get_part ($ msg_count, $ mime_type, $ sub_structure, $ prefix. ($ index + 1); if ($ data) {return $ data ;}}} return false ;} /*** get the subtype and type of the message structure *** @ param object $ structure */private function get_mime_type ($ structure) {$ mime_type = array ("TEXT ", "MULTIPART", "mesparts E "," APPLICATION "," AUDIO "," IMAGE "," VIDEO "," OTHER "); if ($ structure-> subtype) {return $ mime_type [(int) $ structure-> type]. '/'. $ structure-> subtype;} return "TEXT/PLAIN ";} /*** put the message from unread to read ** @ param string $ msg_count * @ return bool */public function mail_read ($ msg_count) {$ status = imap_setflag_full ($ this-> _ connect, $ msg_count, "// Seen"); return $ status ;}/*** Close an IMAP stream */public function close_mail () {imap_close ($ this-> _ connect, CL_EXPUNGE);} function contact_conv ($ contact) {foreach ($ contact as $ vo) {if (isset ($ vo-> personal) {$ tmp = $ tmp. $ this-> mail_decode ($ vo-> personal ). "| ". $ vo-> mailbox. '@'. $ vo-> host. ';} else {$ tmp = $ tmp. $ this-> mail_decode ($ vo-> mailbox ). "| ". $ vo-> mailbox. '@'. $ vo-> host. ';} return $ tmp ;}}?>

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.