Coverage for src/botadi/mokadi/mokadi_mails.py: 54%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Look into the mail box
4"""
5import datetime
6from pyquickhelper.loghelper import noLOG
7from .mokadi_exceptions import MokadiAuthentification
10def enumerate_last_mails(user, pwd, server, nb=5, fLOG=noLOG):
11 """
12 Returned the last mails
14 @param user user
15 @param pwd password
16 @param server server something like ``imap.domain.ext``
17 @param ssl select ``IMPA_SSL`` or ``IMAP``
18 @param nb number of mails to retrieve
19 @param fLOG logging function
21 We use the examples from
22 `Capturing a single image from my webcam in Java or Python
23 <http://stackoverflow.com/questions/11094481/capturing-a-single-image-from-my-webcam-in-java-or-python>`_.
24 See `pymmails <http://www.xavierdupre.fr/app/pymmails/helpsphinx/index.html>`_.
25 """
26 from pymmails.grabber import MailBoxImap
27 now = datetime.datetime.now() - datetime.timedelta(1)
28 date = now.strftime("%d-%b-%Y")
29 pattern = "SINCE %s" % date
30 fLOG("[enumerate_last_mails] pattern ", pattern)
31 box = MailBoxImap(user, pwd, server, ssl=True, fLOG=fLOG)
32 try:
33 box.login()
34 except Exception as e:
35 raise MokadiAuthentification("Unable to connect to the mailbox") from e
37 def enumerate_mails(iter):
38 now = datetime.datetime.utcnow()
39 for mail in iter:
40 dt = mail.get_date()
41 dt = dt.astimezone(now.tzinfo)
42 yield dt, mail
44 mails = box.enumerate_mails_in_folder("inbox", pattern=pattern)
45 for i, (_, mail) in enumerate(sorted(enumerate_mails(mails), reverse=True)):
46 yield mail
47 if i >= nb:
48 break
49 box.logout()