1010use greeny \MailLibrary \Mailbox ;
1111use greeny \MailLibrary \Structures \IStructure ;
1212use greeny \MailLibrary \Structures \ImapStructure ;
13+ use Nette \Utils \Strings ;
1314use greeny \MailLibrary \Mail ;
1415use DateTime ;
1516
@@ -227,18 +228,21 @@ public function checkFilter($key, $value = NULL) {
227228 public function getHeaders ($ mailId )
228229 {
229230 $ raw = imap_fetchheader ($ this ->resource , $ mailId , FT_UID );
230- $ lines = explode ("\n" , $ raw );
231+ $ lines = explode ("\n" , Strings:: fixEncoding ( $ raw) );
231232 $ headers = array ();
232233 $ lastHeader = NULL ;
234+
235+ // normalize headers
233236 foreach ($ lines as $ line ) {
234- if (mb_substr ($ line , 0 , 1 , 'UTF-8 ' ) === " " ) {
235- $ headers [$ lastHeader ] .= $ line ;
237+ $ firstCharacter = mb_substr ($ line , 0 , 1 , 'UTF-8 ' ); // todo: correct assumption that string must be UTF-8 encoded?
238+ if (preg_match ('/[\pZ\pC]/u ' , $ firstCharacter ) === 1 ) { // search for UTF-8 whitespaces
239+ $ headers [$ lastHeader ] .= " " . Strings::trim ($ line );
236240 } else {
237241 $ parts = explode (': ' , $ line );
238- $ name = $ parts [0 ];
242+ $ name = Strings:: trim ( $ parts [0 ]) ;
239243 unset($ parts [0 ]);
240244
241- $ headers [$ name ] = implode (': ' , $ parts );
245+ $ headers [$ name ] = Strings:: trim ( implode (': ' , $ parts) );
242246 $ lastHeader = $ name ;
243247 }
244248 }
@@ -254,7 +258,7 @@ public function getHeaders($mailId)
254258 $ text = '' ;
255259 foreach ($ decoded as $ part ) {
256260 if ($ part ->charset !== 'UTF-8 ' && $ part ->charset !== 'default ' ) {
257- $ text .= mb_convert_encoding ($ part ->text , 'UTF-8 ' , $ part ->charset );
261+ $ text .= @ mb_convert_encoding ($ part ->text , 'UTF-8 ' , $ part ->charset ); // todo: handle this more properly
258262 } else {
259263 $ text .= $ part ->text ;
260264 }
0 commit comments