mail: decode multipart attachments (text, html, images)
[minimedit.git] / mail / index.php
index ee7365aa62cb321de3af6581704edea85cf063e2..9f570640e8b5862ee3908bf6af6140db7d515604 100644 (file)
@@ -36,21 +36,65 @@ if ($msgid) {
        );
        print '</dl>';
 
-       if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
+       $type = preg_replace('/;.*/', '', $head['Content-Type'] ?? 'text/plain', 1);
+       switch ($type) {
+       case 'text/plain':
                $body = $rawbody;
                if (($head['Content-Transfer-Encoding'] ?? '') === 'quoted-printable') {
                        $body = quoted_printable_decode($body);
                }
-               printf('<pre>%s</pre>', htmlspecialchars($body));
-       }
-       else {
-               printf('<p>Geen ondersteuning voor <em>%s</em>.</p>', htmlspecialchars($head['Content-Type']));
-
-               /* TODO
+               $body = nl2br(htmlspecialchars($body));
+               print "<p>$body</p>\n";
+               break;
+       case 'multipart/alternative':
                $mime = mailparse_msg_parse_file($filename);
-               $part = mailparse_msg_get_part($mime, '1');
-               mailparse_msg_extract_part_file($part, $filename);
-               */
+               foreach (mailparse_msg_get_structure($mime) as $section) {
+                       print "<h3>$section</h3>\n";
+                       $part = mailparse_msg_get_part($mime, $section);
+                       if ($section === '1') {
+                               continue; // do not access container to prevent segfault
+                       }
+                       $pinfo = mailparse_msg_get_part_data($part);
+               #printf('<pre>%s</pre>', htmlspecialchars(print_r($pinfo,TRUE)));
+                       $phead = $pinfo['headers'];
+                       list ($ptype) = explode(';', strtolower($phead['content-type']));
+                       list ($pmedia) = explode('/', $ptype);
+                       switch ($pmedia) {
+                       case 'multipart':
+                               continue 2;
+                       case 'text':
+                               $body = mailparse_msg_extract_part_file($part, $filename, NULL);
+                               #iconv($pinfo['charset'], 'UTF-8', $body); #TODO test if needed
+                               switch ($ptype) {
+                               case 'text/plain':
+                                       $body = htmlspecialchars($body);
+                                       print "<pre><p>$body</p></pre>\n";
+                                       break;
+                               case 'text/html':
+                                       $body = preg_replace('{
+                                               (<br */?>) | <[^>]+> # keep line breaks, replace other elements
+                                       }x', "\\1\n", $body); # strip_tags
+                                       print "<p>$body</p>\n";
+                                       break;
+                               }
+                               break;
+                       case 'image':
+                               if (!preg_match('/^inline;/', $phead['content-disposition'])) {
+                                       continue 2;
+                               }
+                               $name = iconv_mime_decode($phead['disposition-filename']);
+                               $body = mailparse_msg_extract_part_file($part, $filename, NULL);
+                               printf('<img src="data:%s;base64,%s" alt="%s" />',
+                                       $ptype, base64_encode($body),
+                                       htmlspecialchars($phead['name'] ?? '-')
+                               );
+                               break;
+                       }
+               }
+               mailparse_msg_free($mime);
+               break;
+       default:
+               printf('<p>Geen ondersteuning voor <em>%s</em>.</p>', htmlspecialchars($type));
        }
        return;
 }