bamthomas / aioimaplib

Python asyncio IMAP4rev1 client library
GNU General Public License v3.0
135 stars 58 forks source link

How to read e-mail content? #95

Open tom634 opened 1 year ago

tom634 commented 1 year ago

In the documentation I couldn't find example for printing mail content. I want to print e-mail content. I am using part of the code from documentation:

async def idle_loop(host, user, password):
   imap_client = aioimaplib.IMAP4_SSL(host=host, timeout=30)
   await imap_client.wait_hello_from_server()

   await imap_client.login(user, password)
   await imap_client.select()

   while True:
      print((await imap_client.uid('fetch', '1:*', 'FLAGS')))

      idle = await imap_client.idle_start(timeout=60)
      print((await imap_client.wait_server_push()))

      imap_client.idle_done()
      await asyncio.wait_for(idle, 30)

this code works, but how to read last e-mail content? My example response for line: print((await imap_client.uid('fetch', '1:*', 'FLAGS'))) is:

Response(result='OK', lines=[b'1 FETCH (FLAGS () UID 5)', b'2 FETCH (FLAGS (\\Seen \\Answered) UID 10)', b'3 FETCH (FLAGS (\\Seen \\Answered) UID 13)', b'4 FETCH (FLAGS (\\Seen \\Answered) 
UID 16)', b'5 FETCH (FLAGS () UID 19)', b'6 FETCH (FLAGS () UID 21)', b'7 FETCH (FLAGS () UID 25)', b'8 FETCH (FLAGS () UID 27)', b'9 FETCH (FLAGS (\\Seen \\Answered) UID 35)', b'10 FETCH (FLAGS (\\Seen \\Answered) UID 38)', b'11 FETCH (FLAGS (\\Seen \\Answered) UID 41)', b'12 FETCH (FLAGS (\\Seen \\Answered) UID 44)', b'13 FETCH (FLAGS (\\Seen \\Answered) UID 47)', b'14 FETCH (FLAGS (\\Seen \\Answered) UID 50)', b'15 FETCH (FLAGS (\\Seen \\Answered) UID 53)', b'16 FETCH (FLAGS (\\Seen \\Answered) UID 56)', b'17 FETCH (FLAGS (\\Seen \\Answered) UID 62)', b'18 FETCH (FLAGS (\\Seen \\Answered) UID 
65)', b'19 FETCH (FLAGS () UID 68)', b'20 FETCH (FLAGS () UID 70)', b'21 FETCH (FLAGS () UID 72)', b'22 FETCH (FLAGS () UID 74)', b'23 FETCH (FLAGS () UID 76)', b'24 FETCH (FLAGS () UID 78)', b'25 FETCH (FLAGS () UID 80)', b'26 FETCH (FLAGS () UID 82)', b'27 FETCH (FLAGS () UID 84)', b'28 FETCH (FLAGS () UID 86)', b'29 FETCH (FLAGS () UID 88)', b'30 FETCH (FLAGS () UID 90)', b'31 FETCH (FLAGS () UID 92)', b'FETCH completed.'])

and for line: print((await imap_client.wait_server_push())) is: [b'1 RECENT', b'33 EXISTS'] I am using outlook mail service.

Ubya-tech commented 1 year ago

to get the body of an email you need to use: await imap_client.uid('fetch', str(uid), 'BODY.PEEK[]') https://github.com/bamthomas/aioimaplib/blob/master/example/imap_fetch.py In this example they used some regex expressions to first extract the UID, and then use it in the above code. after that you'd still need some manipulations to extract what you need