只在imaplib中显示特定数量的电子邮件 [英] Only show certain number of emails in imaplib

查看:236
本文介绍了只在imaplib中显示特定数量的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的代码工作很好,但我测试这个测试电子邮件,它只有大约5个电子邮件。
在我的实际电子邮件帐户中,我在收件箱中至少有4k封电子邮件。

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.

我有一种方法可以显示一定数量的电子邮件。例如,只显示前20个最新的电子邮件?然后,后说,当一个按钮被点击它将显示下20个电子邮件...

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

如果 id_list [-1] 可以得到最新的电子邮件ID,我可以做类似 id_list [-1] + 19 获取最近的20封电子邮件?

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.

到目前为止,我有

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

最新更新:

我将代码更新为以下内容:

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

first
second
third
fourth
.
.
.
.
tenth

所以我想我添加 -1 ,以便按照降序排序

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

推荐答案

我只是意识到我已经有了我想要的输出,指定数量的电子邮件。

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

其中 id_list [0:10]:将在我的收件箱中输出前10封电子邮件。如果我需要输出20,它可以修改为 id_list [0:20] 我有另一个问题,但我会问一个不同的职位。但到目前为止,我有一半的需要,所以我将在这里发布我的解决方案。

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.

这篇关于只在imaplib中显示特定数量的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆