@@ -226,7 +226,7 @@ public function __construct($uid, $msglist, Client $client, $fetch_options = nul
226226 $ this ->msglist = $ msglist ;
227227 $ this ->client = $ client ;
228228
229- $ this ->uid = ( $ this -> fetch_options == IMAP :: FT_UID ) ? $ uid : $ uid ;
229+ $ this ->uid = $ uid ;
230230 $ this ->msgn = ($ this ->fetch_options == IMAP ::FT_UID ) ? \imap_msgno ($ this ->client ->getConnection (), $ uid ) : $ uid ;
231231
232232 $ this ->parseHeader ();
@@ -619,25 +619,8 @@ private function parseAddresses($list) {
619619 */
620620 public function parseBody () {
621621 $ this ->client ->openFolder ($ this ->folder_path );
622- $ this ->structure = \imap_fetchstructure ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
623-
624- if (property_exists ($ this ->structure , 'parts ' )){
625- $ parts = $ this ->structure ->parts ;
626-
627- foreach ($ parts as $ part ) {
628- foreach ($ part ->parameters as $ parameter ) {
629- if ($ parameter ->attribute == "charset " ) {
630- $ encoding = $ parameter ->value ;
631-
632- $ encoding = preg_replace ('/Content-Transfer-Encoding/ ' , '' , $ encoding );
633- $ encoding = preg_replace ('/iso-8859-8-i/ ' , 'iso-8859-8 ' , $ encoding );
634-
635- $ parameter ->value = $ encoding ;
636- }
637- }
638- }
639- }
640622
623+ $ this ->structure = \imap_fetchstructure ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
641624 $ this ->fetchStructure ($ this ->structure );
642625
643626 return $ this ;
@@ -658,56 +641,10 @@ private function fetchStructure($structure, $partNumber = null) {
658641 (empty ($ structure ->disposition ) || strtolower ($ structure ->disposition ) != 'attachment ' )
659642 ) {
660643 if (strtolower ($ structure ->subtype ) == "plain " || strtolower ($ structure ->subtype ) == "csv " ) {
661- if (!$ partNumber ) {
662- $ partNumber = 1 ;
663- }
664-
665- $ encoding = $ this ->getEncoding ($ structure );
666-
667- $ content = \imap_fetchbody ($ this ->client ->getConnection (), $ this ->uid , $ partNumber , $ this ->fetch_options | IMAP ::FT_UID );
668- $ content = $ this ->decodeString ($ content , $ structure ->encoding );
669-
670- // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
671- // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
672- // https://stackoverflow.com/a/11303410
673- //
674- // us-ascii is the same as ASCII:
675- // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
676- // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
677- // based on the typographical symbols predominantly in use there.
678- // https://en.wikipedia.org/wiki/ASCII
679- //
680- // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
681- if ($ encoding != 'us-ascii ' ) {
682- $ content = $ this ->convertEncoding ($ content , $ encoding );
683- }
684-
685- $ body = new \stdClass ;
686- $ body ->type = "text " ;
687- $ body ->content = $ content ;
688-
689- $ this ->bodies ['text ' ] = $ body ;
690-
644+ $ this ->bodies ['text ' ] = $ this ->createBody ("text " , $ structure , $ partNumber );
691645 $ this ->fetchAttachment ($ structure , $ partNumber );
692-
693646 } elseif (strtolower ($ structure ->subtype ) == "html " ) {
694- if (!$ partNumber ) {
695- $ partNumber = 1 ;
696- }
697-
698- $ encoding = $ this ->getEncoding ($ structure );
699-
700- $ content = \imap_fetchbody ($ this ->client ->getConnection (), $ this ->uid , $ partNumber , $ this ->fetch_options | IMAP ::FT_UID );
701- $ content = $ this ->decodeString ($ content , $ structure ->encoding );
702- if ($ encoding != 'us-ascii ' ) {
703- $ content = $ this ->convertEncoding ($ content , $ encoding );
704- }
705-
706- $ body = new \stdClass ;
707- $ body ->type = "html " ;
708- $ body ->content = $ content ;
709-
710- $ this ->bodies ['html ' ] = $ body ;
647+ $ this ->bodies ['html ' ] = $ this ->createBody ("html " , $ structure , $ partNumber );
711648 } elseif ($ structure ->ifdisposition == 1 && strtolower ($ structure ->disposition ) == 'attachment ' ) {
712649 if ($ this ->getFetchAttachmentOption () === true ) {
713650 $ this ->fetchAttachment ($ structure , $ partNumber );
@@ -721,11 +658,57 @@ private function fetchStructure($structure, $partNumber = null) {
721658 }
722659 $ this ->fetchStructure ($ subStruct , $ prefix .($ index + 1 ));
723660 }
724- } else {
725- if ($ this ->getFetchAttachmentOption () === true ) {
726- $ this ->fetchAttachment ($ structure , $ partNumber );
727- }
661+ } else if ($ this ->getFetchAttachmentOption () === true ) {
662+ $ this ->fetchAttachment ($ structure , $ partNumber );
663+ }
664+ }
665+
666+ /**
667+ * Create a new body object of a given type
668+ * @param string $type
669+ * @param object $structure
670+ * @param mixed $partNumber
671+ *
672+ * @return object
673+ * @throws Exceptions\ConnectionFailedException
674+ */
675+ private function createBody ($ type , $ structure , $ partNumber ){
676+ return (object ) [
677+ "type " => $ type ,
678+ "content " => $ this ->fetchPart ($ structure , $ partNumber ),
679+ ];
680+ }
681+
682+ /**
683+ * Fetch the content of a given part and message structure
684+ * @param object $structure
685+ * @param mixed $partNumber
686+ *
687+ * @return mixed|string
688+ * @throws Exceptions\ConnectionFailedException
689+ */
690+ private function fetchPart ($ structure , $ partNumber ){
691+ $ encoding = $ this ->getEncoding ($ structure );
692+
693+ $ content = \imap_fetchbody ($ this ->client ->getConnection (), $ this ->uid , $ partNumber | 1 , $ this ->fetch_options | IMAP ::FT_UID );
694+ $ content = $ this ->decodeString ($ content , $ structure ->encoding );
695+
696+ // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
697+ // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
698+ // https://stackoverflow.com/a/11303410
699+ //
700+ // us-ascii is the same as ASCII:
701+ // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
702+ // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
703+ // based on the typographical symbols predominantly in use there.
704+ // https://en.wikipedia.org/wiki/ASCII
705+ //
706+ // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
707+ if ($ encoding != 'us-ascii ' ) {
708+ $ content = $ this ->convertEncoding ($ content , $ encoding );
728709 }
710+
711+ return $ content ;
729712 }
730713
731714 /**
@@ -831,8 +814,6 @@ public function setFetchFlagsOption($option) {
831814 */
832815 public function decodeString ($ string , $ encoding ) {
833816 switch ($ encoding ) {
834- case IMAP ::MESSAGE_ENC_7BIT :
835- return $ string ;
836817 case IMAP ::MESSAGE_ENC_8BIT :
837818 return quoted_printable_decode (\imap_8bit ($ string ));
838819 case IMAP ::MESSAGE_ENC_BINARY :
@@ -841,8 +822,8 @@ public function decodeString($string, $encoding) {
841822 return \imap_base64 ($ string );
842823 case IMAP ::MESSAGE_ENC_QUOTED_PRINTABLE :
843824 return quoted_printable_decode ($ string );
825+ case IMAP ::MESSAGE_ENC_7BIT :
844826 case IMAP ::MESSAGE_ENC_OTHER :
845- return $ string ;
846827 default :
847828 return $ string ;
848829 }
@@ -911,7 +892,7 @@ public function getEncoding($structure) {
911892 return mb_detect_encoding ($ structure );
912893 }
913894
914- return ' UTF-8 ' ;
895+ return $ this -> fallback_encoding ;
915896 }
916897
917898 /**
@@ -1168,20 +1149,18 @@ public function getStructure(){
11681149 /**
11691150 * Does this message match another one?
11701151 *
1171- * A match means same uid, message id, subject and date/time.
1152+ * A match means same uid, message id, subject, body length and date/time.
11721153 *
11731154 * @param null|Message $message
1174- * @return boolean
1155+ * @return bool
1156+ * @throws Exceptions\ConnectionFailedException
11751157 */
11761158 public function is (Message $ message = null ) {
11771159 if (is_null ($ message )) {
11781160 return false ;
11791161 }
11801162
1181- return $ this ->uid == $ message ->uid
1182- && $ this ->message_id == $ message ->message_id
1183- && $ this ->subject == $ message ->subject
1184- && $ this ->date ->eq ($ message ->date );
1163+ return $ this ->getToken () == $ message ->getToken () && $ this ->date ->eq ($ message ->date );
11851164 }
11861165
11871166 /**
0 commit comments