仅从电子邮件中检索新邮件

时间:2011-12-05 05:13:35

标签: python imap imaplib

我正在尝试使用Imaplib从电子邮件地址检索邮件,到目前为止我已经能够检索邮件,但是,我只对原始邮件感兴趣,这意味着如果邮件是“Re:”那么我不想要以前的消息,我只想要新消息,这可能吗?

继承人我从python获得的东西:

test # 10\r\n\r\nOn Thu, Dec 1, 2011 at 11:17 PM, paulo alvarado\r\n<commonzenpython@gmail.com>wrote:\r\n\r\n> test # 9\r\n>\r\n>\r\n> On Thu, Dec 1, 2011 at 11:15 PM, paulo alvarado <commonzenpython@gmail.com\r\n> > wrote:\r\n>\r\n>> test # 8\r\n>>\r\n>>\r\n>> On Thu, Dec 1, 2011 at 11:14 PM, paulo alvarado <\r\n>> commonzenpython@gmail.com> wrote:\r\n>>\r\n>>> test # 7\r\n>>>\r\n>>> On Thu, Dec 1, 2011 at 10:36 PM, paulo alvarado <\r\n>>> commonzenpython@gmail.com> wrote:\r\n>>>\r\n>>>> test # 6\r\n>>>>\r\n>>>>\r\n>>>> On Thu, Dec 1, 2011 at 10:36 PM, paulo alvarado <\r\n>>>> commonzenpython@gmail.com> wrote:\r\n>>>>\r\n>>>>> test # 5\r\n>>>>>\r\n>>>>>\r\n>>>>> On Thu, Dec 1, 2011 at 9:46 PM, paulo alvarado <\r\n>>>>> commonzenpython@gmail.com> wrote:\r\n>>>>>\r\n>>>>>> this is test # 4\r\n>>>>>>\r\n>>>>>>\r\n>>>>>>\r\n>>>>>> On Thu, Dec 1, 2011 at 7:13 PM, paulo alvarado <\r\n>>>>>> commonzenpython@gmail.com> wrote:\r\n>>>>>>\r\n>>>>>>> this is test # 1\r\n>>>>>>>\r\n>>>>>>\r\n>>>>>>\r\n>>>>>\r\n>>>>\r\n>>>\r\n>>\r\n>\r\n

正如您所看到的,测试#10是新消息,其他内容是之前的回复 我正在使用python2.7

修改

我有一个票务应用程序,所以基本上,我希望用户能够通过电子邮件提交票证,因此用户的电子邮件是作者,主题是标题,消息正文是描述,我有除了如何处理回复之外,一切正常,因为此时,如果用户通过电子邮件回复,则故障单的描述不仅包含新问题(或响应),而且还包含先前的问题,因此我想知道是否有办法删除以前的回复,只保留新回复,这是我的问题的图片。

As you can see, the description of the ticket contains the email's previous replies

1 个答案:

答案 0 :(得分:-1)

谷歌提出了

import imaplib
conn = imaplib.IMAP4_SSL(host='mail.example.com')
# or conn =imaplib.IMAP4(host='mail.example.com') for no SSL 
try: 
    (retcode, capabilities) = conn.login('user', 'pass') 
except: 
    # cannot login
conn.select(readonly=1) # Select inbox or default namespace 
(retcode, messages) = conn.search(None, '(UNSEEN)') 
if retcode == 'OK': 
    for message in messages[0].split(' '): 
        print 'Processing :', message 
        (ret, mesginfo) = conn.fetch(message, '(BODY[HEADER.FIELDS (SUBJECT FROM)])') 
        if ret == 'OK': 
            # process message here 
conn.close() 

其中conn.search(None, '(UNSEEN)')是代码中的重要部分。