mail: imap function to parse sender addresses
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 6 Dec 2020 06:31:32 +0000 (07:31 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 19 Dec 2020 02:01:43 +0000 (03:01 +0100)
Require built-in imap module instead of the mailparse extension.
Either can be installed on Debian (or enabled on Vimexx servers)
so prefer the more complete solution.

mail/index.php

index ce133c5943797cc38a62803e77aa0e8e9fb79493..b4846cc8f48b5c4f8e21420562175a44eade63e9 100644 (file)
@@ -6,8 +6,10 @@ function parsemailhead($headerdata)
 {
        $headlist = iconv_mime_decode_headers($headerdata, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
        $headlist['date'] = DateTime::createFromFormat(DateTimeInterface::RFC2822.'+', $headlist['Date']);
-       $headlist['from'] = mailparse_rfc822_parse_addresses($headlist['From']);
-       //TODO: imap_rfc822_parse_adrlist() alternative
+       $headlist['from'] = imap_rfc822_parse_adrlist($headlist['From'], '');
+       array_walk($headlist['from'], function ($row) {
+               $row->display = $row->personal ?? $row->mailbox;
+       });
        return $headlist;
 }
 
@@ -21,7 +23,7 @@ if ($msgid) {
 
        print '<dl class="terse">';
        printf('<dt>Ontvangen:</dt><dd>%s</dd>', $head['date']->format('c'));
-       printf('<dt>Verzender:</dt><dd>%s</dd>', htmlspecialchars($head['from'][0]['display']));
+       printf('<dt>Verzender:</dt><dd>%s</dd>', htmlspecialchars($head['From']));
        print '</dl>';
 
        if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
@@ -72,7 +74,9 @@ foreach (array_reverse($rows) as $filename) {
        printf(' <small class="date">%s</small>',
                showdate(explode('-', $head['date']->format('Y-m-d')))
        );
-       print ' <em class="right">'.htmlspecialchars($head['from'][0]['display']).'</em>';
+       printf(' <em class="right">%s</em>',
+               htmlspecialchars(implode(', ', array_column($head['from'], 'display')))
+       );
        print "</a></li>\n";
 }
 print "</ul>\n";