mail: imap function to parse sender addresses
[minimedit.git] / mail / index.php
1 <?php
2 $mailbox = 'mail/inbox';
3 @list ($msgid) = explode('/', ltrim($Page->path, '/'));
4
5 function parsemailhead($headerdata)
6 {
7         $headlist = iconv_mime_decode_headers($headerdata, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
8         $headlist['date'] = DateTime::createFromFormat(DateTimeInterface::RFC2822.'+', $headlist['Date']);
9         $headlist['from'] = imap_rfc822_parse_adrlist($headlist['From'], '');
10         array_walk($headlist['from'], function ($row) {
11                 $row->display = $row->personal ?? $row->mailbox;
12         });
13         return $headlist;
14 }
15
16 if ($msgid) {
17         $filename = "$mailbox/$msgid";
18         list ($headerdata, $rawbody) = explode("\n\n", file_get_contents($filename), 2);
19         $head = parsemailhead($headerdata);
20
21         $Page->title = 'Mailbericht ' . $head['date']->format('Y-m-d H:i');
22         printf("<h2>%s</h2>\n", htmlspecialchars($head['Subject'] ?? 'Mailbericht zonder onderwerp'));
23
24         print '<dl class="terse">';
25         printf('<dt>Ontvangen:</dt><dd>%s</dd>', $head['date']->format('c'));
26         printf('<dt>Verzender:</dt><dd>%s</dd>', htmlspecialchars($head['From']));
27         print '</dl>';
28
29         if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
30                 $body = $rawbody;
31                 if (($head['Content-Transfer-Encoding'] ?? '') === 'quoted-printable') {
32                         $body = quoted_printable_decode($body);
33                 }
34                 printf('<pre>%s</pre>', htmlspecialchars($body));
35         }
36         else {
37                 printf('<p>Geen ondersteuning voor <em>%s</em>.</p>', htmlspecialchars($head['Content-Type']));
38
39                 /* TODO
40                 $mime = mailparse_msg_parse_file($filename);
41                 $part = mailparse_msg_get_part($mime, '1');
42                 mailparse_msg_extract_part_file($part, $filename);
43                 */
44         }
45         return;
46 }
47
48 if (!$User->admin('user')) {
49         http_response_code(403);
50         $Page->place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
51         $Page->place['maillist'] = '';
52         return TRUE;
53 }
54
55 $rows = glob("$mailbox/*");
56 if (!$rows) {
57         throw new Exception('Kon inbox niet openen.');
58 }
59 array_splice($rows, 0, -50);
60
61 ob_start();
62 print '<ul>';
63 foreach (array_reverse($rows) as $filename) {
64         if (!is_readable($filename)) {
65                 continue;
66         }
67
68         printf('<li><a href="%s">', "/{$Page->handler}/".basename($filename));
69
70         list ($headerdata) = explode("\n\n", file_get_contents($filename));
71         $head = parsemailhead($headerdata);
72
73         print $head['Subject'];
74         printf(' <small class="date">%s</small>',
75                 showdate(explode('-', $head['date']->format('Y-m-d')))
76         );
77         printf(' <em class="right">%s</em>',
78                 htmlspecialchars(implode(', ', array_column($head['from'], 'display')))
79         );
80         print "</a></li>\n";
81 }
82 print "</ul>\n";
83 $Page->place['maillist'] = ob_get_clean();