Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read e-mail content? #95

Open
tom634 opened this issue Jul 18, 2023 · 1 comment
Open

How to read e-mail content? #95

tom634 opened this issue Jul 18, 2023 · 1 comment

Comments

@tom634
Copy link

tom634 commented Jul 18, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants