Only show certain number of emails in imaplib

2019-08-23 07:09发布

问题:

The code I have below is working great but I am testing this on a test email and it only have around 5 emails. On my actual email account I have atleast 4k emails on my inbox.

Is there a way for me to show for a certain number of emails. For example, only show the first 20 most recent emails? And then after that say when a button is clicked it will show the next 20 emails...

import sys
import imaplib
import getpass
import email
import email.header
import datetime


email_address = "email@company.net"
password = "123456"

M = imaplib.IMAP4('mail.company.net')
rows = []

try:
    M.login(email_address, password)
    print "LOGIN SUCCESS!"
except imaplib.IMAP4.error:
    print "LOGIN FAILED!!"

rv, data = M.select("INBOX")
rv, data = M.search(None, "ALL")

for num in data[0].split():
    rv, data = M.fetch(num, '(RFC822)')
    msg = email.message_from_string(data[0][1])

    subj = msg['Subject']
    to = msg['To']
    frm = msg['From']
    body = msg.get_payload()

    print subj, " ", to, " ", frm, " ", body

M.close()
M.logout()

I'm sorry but I'm really having a hard time trying to figure this out. Most recent I learned is that I can get the total number of emails

num_msgs = int(data[0])
print 'You have a total of %d messages' % num_msgs

And if id_list[-1] can get the latest email id, can i do something like id_list[-1] + 19 or something so it can get the 20 most recent email?

I would really appreciate any help on how to achieve my desired output. Thank You.

So far I have

ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1] #gets most recent email

for i in latest_email_id:
    if i > 21:
        rv, data = M.fetch(num, '(RFC822)')
        msgrecent = email.message_from_string(data[0][1])
        subjs = msgrecent['Subject']
        print "only print 20 most recent email"
        print subjs
    else:
        print "none"

How can i modify this to get the output i need? Thank you


Latest update:

I updated the code to the following:

ids = data[0]
id_list = ids.split()

for num in id_list[0:10]:
    rv, data = M.fetch(num, '(RFC822)')
    msg = email.message_from_string(data[0][1])

    subj = msg['Subject']
    to = msg['To']
    frm = msg['From']
    body = msg.get_payload()

    print subj

This outputs in ascending order

first
second
third
fourth
.
.
.
.
tenth

so i figured i add -1 so that it will sort in descending order

ids = data[0]
id_list = ids.split()

for num in id_list[0:10:-1]:
    rv, data = M.fetch(num, '(RFC822)')
    msg = email.message_from_string(data[0][1])

    subj = msg['Subject']
    to = msg['To']
    frm = msg['From']
    body = msg.get_payload()

    print subj

but when i did that, i didn't get any output.

Any idea on how i can fix this? Thank You

回答1:

I just realized that I already have the output that I want, which is to only output a specified number of emails.

ids = data[0]
id_list = ids.split()

for num in id_list[0:10]:
    rv, data = M.fetch(num, '(RFC822)')
    msg = email.message_from_string(data[0][1])

    subj = msg['Subject']
    to = msg['To']
    frm = msg['From']
    body = msg.get_payload()

    print subj

Wherein id_list[0:10]: will output the first 10 email in my inbox. If i need to output 20, it can be modified to id_list[0:20] I have another issue but I will ask that on a different post. But so far, I have half of what I need so I will post my solution here.