+from mailbox import Maildir
+from email.header import decode_header
+from flask import render_template
from app import app
-@app.route('/')
-@app.route('/index')
+@app.route('/mails/<user>/')
+@app.route('/mails/<user>/<subdir>')
+def listMails(user, subdir=""):
+ base_path = "/srv/mail"
+ messages = Maildir(f"{base_path}/{user}/{subdir}")
-def index():
- return "Hello World"
+ return render_template("maildir.html", messages=messages)
+
+
+@app.route('/mails/<user>/show/<mail_ID>')
+@app.route('/mails/<user>/{subdir}/show/<mail_ID>')
+def showMail(user, mail_ID, subdir=""):
+ base_path = "/srv/mail"
+ mbox = Maildir(f"{base_path}/{user}/{subdir}")
+
+ message = mbox.get(mail_ID)
+
+# if message.is_multipart():
+# for part in message.get_payload():
+# body = part.get_payload()
+# else:
+# body = message.get_payload()
+
+ return render_template("show_mail.html", message=message)
--- /dev/null
+<html>
+ <head>
+ <title>Maildir listing</title>
+ </head>
+ <body>
+ <table>
+ <tr>
+ <th>Sender</th>
+ <th>Subject</th>
+ </tr>
+ {% for message in messages %}
+ <tr>
+ <td>{{ message['from'] }} </td>
+ <td>{{ message['subject'] }}</td>
+ </tr>
+ {% endfor %}
+ </table>
+ </body>
+</html>
--- /dev/null
+<html>
+ <head>
+ <title>{{ message['Subject'] }}</title>
+ </head>
+ <body>
+ <table>
+ <tr>
+ <th>MIME</th>
+ <th>Content</th>
+ </tr>
+ <tr>
+ {% if message.is_multipart %}
+ {% for body in message.get_payload %}
+ {% if body.get_content_maintype == 'text' %}
+ <td>{{ body.get_content_type }}</td>
+ <td>{{ body.as_string() }} </td>
+ {% else %}
+ <td>{{ body.get_content_type }}</td>
+ <td>Currently only text bodies are supported!</td>
+ {% endif %}
+ {% endfor %}
+ {% else %}
+ {% if message.get_content_maintype == 'text' %}
+ <td>{{ message.get_content_type }}</td>
+ <td>{{ message.as_string() }} </td>
+ {% else %}
+ <td>{{ message.get_content_type }}</td>
+ <td>Currently only text bodies are supported!</td>
+ {% endif %}
+ {% endif %}
+ </tr>
+ </table>
+ </body>
+</html>