@@ -241,4 +241,90 @@ function getTrasporto()
241241 {
242242 return $ this ->trasporto ;
243243 }
244+
245+ /**
246+ * La funzione originale non tiene conto del problema delle email che contengono
247+ * altre email.
248+ *
249+ * Allo stato attuale la soluzione è già stata proposta (https://github.com/tedious/Fetch/pull/201)
250+ * ma non ancora mergiata.
251+ *
252+ * This function takes in a structure and identifier and processes that part of the message. If that portion of the
253+ * message has its own subparts, those are recursively processed using this function.
254+ *
255+ * @param \stdClass $structure
256+ * @param string $partIdentifier
257+ */
258+ protected function processStructure ($ structure , $ partIdentifier = null )
259+ {
260+ $ parameters = self ::getParametersFromStructure ($ structure );
261+
262+ if ((isset ($ parameters ['name ' ]) || isset ($ parameters ['filename ' ]))
263+ || (isset ($ structure ->subtype ) && strtolower ($ structure ->subtype ) == 'rfc822 ' )
264+ ) {
265+ $ attachment = new Attachment ($ this , $ structure , $ partIdentifier );
266+ $ this ->attachments [] = $ attachment ;
267+ } elseif ($ structure ->type == 0 || $ structure ->type == 1 ) {
268+ $ messageBody = isset ($ partIdentifier ) ?
269+ imap_fetchbody ($ this ->imapStream , $ this ->uid , $ partIdentifier , FT_UID | FT_PEEK )
270+ : imap_body ($ this ->imapStream , $ this ->uid , FT_UID | FT_PEEK );
271+
272+ $ messageBody = self ::decode ($ messageBody , $ structure ->encoding );
273+
274+ if (!empty ($ parameters ['charset ' ]) && $ parameters ['charset ' ] !== self ::$ charset ) {
275+ $ mb_converted = false ;
276+ if (function_exists ('mb_convert_encoding ' )) {
277+ if (!in_array ($ parameters ['charset ' ], mb_list_encodings ())) {
278+ if ($ structure ->encoding === 0 ) {
279+ $ parameters ['charset ' ] = 'US-ASCII ' ;
280+ } else {
281+ $ parameters ['charset ' ] = 'UTF-8 ' ;
282+ }
283+ }
284+
285+ $ messageBody = @mb_convert_encoding ($ messageBody , self ::$ charset , $ parameters ['charset ' ]);
286+ $ mb_converted = true ;
287+ }
288+ if (!$ mb_converted ) {
289+ $ messageBodyConv = @iconv ($ parameters ['charset ' ], self ::$ charset . self ::$ charsetFlag , $ messageBody );
290+
291+ if ($ messageBodyConv !== false ) {
292+ $ messageBody = $ messageBodyConv ;
293+ }
294+ }
295+ }
296+
297+ if (strtolower ($ structure ->subtype ) === 'plain ' || ($ structure ->type == 1 && strtolower ($ structure ->subtype ) !== 'alternative ' )) {
298+ if (isset ($ this ->plaintextMessage )) {
299+ $ this ->plaintextMessage .= PHP_EOL . PHP_EOL ;
300+ } else {
301+ $ this ->plaintextMessage = '' ;
302+ }
303+
304+ $ this ->plaintextMessage .= trim ($ messageBody );
305+ } elseif (strtolower ($ structure ->subtype ) === 'html ' ) {
306+ if (isset ($ this ->htmlMessage )) {
307+ $ this ->htmlMessage .= '<br><br> ' ;
308+ } else {
309+ $ this ->htmlMessage = '' ;
310+ }
311+
312+ $ this ->htmlMessage .= $ messageBody ;
313+ }
314+ }
315+
316+ if (! empty ($ structure ->parts )) {
317+ if (isset ($ structure ->subtype ) && strtolower ($ structure ->subtype ) === 'rfc822 ' ) {
318+ $ this ->processStructure ($ structure ->parts [0 ], $ partIdentifier );
319+ } else {
320+ // multipart: iterate through each part
321+ foreach ($ structure ->parts as $ partIndex => $ part ) {
322+ $ partId = $ partIndex + 1 ;
323+ if (isset ($ partIdentifier ))
324+ $ partId = $ partIdentifier . '. ' . $ partId ;
325+ $ this ->processStructure ($ part , $ partId );
326+ }
327+ }
328+ }
329+ }
244330}
0 commit comments