Gentoo Archives: gentoo-user

From: Daniel Quinn <gentoo@×××××××××××.org>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] 2000 emails - printing, sorting by date
Date: Wed, 17 Aug 2016 14:12:18
Message-Id: f92be09c-1ab6-171f-c442-f0763ec446b6@danielquinn.org
In Reply to: [gentoo-user] 2000 emails - printing, sorting by date by Stroller
1 I’m a Python guy, so my answer to this would be "use Python" :-)
2
3 [The ReportLab
4 library](https://www.reportlab.com/docs/reportlab-userguide.pdf) is
5 extremely powerful and can be used to generate a PDF for every email or
6 a pdf for all emails. I've not used it myself, but I hear it's very good.
7
8 That’s the hard part really. Outside of that, you’d just use something
9 like this:
10
11 ```python
12 import os
13 from email import policy
14 from email.parser import BytesParser
15
16 maildir = "/path/to/maildir"
17 messages = []
18
19 for mail in os.listdir(maildir):
20 with open(os.path.join(maildir, mail)) as f:
21 raw = f.read()
22 message = BytesParser(policy=policy.default).parsebytes(f.read())
23 if "someone@××××××××××.com" in raw:
24 messages.append(message)
25 ```
26
27 Once you've created a collection of email objects, you can use the
28 powers of the email module to easily parse out the bits you want. You
29 can take a look at some code I wrote that does just that
30 [here](https://github.com/UKTradeInvestment/barbara/blob/master/interactions/mail.py#L27).
31 Once you've parsed the message, you can then sort your list based on the
32 date. Something like this:
33
34 ```python
35 messages.sort(key=lambda _: _["Date"])
36 ```
37
38 At that point you have a sorted list of email objects which you can then
39 use ReportLab to generate a PDF.
40
41 Good luck :-)

Replies

Subject Author
Re: [gentoo-user] 2000 emails - printing, sorting by date Stroller <stroller@××××××××××××××××××.uk>