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

发布于 2024-12-19 15:34:29 字数 2975 浏览 0 评论 0原文

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

这是我从 python 得到的:

test # 10\r\n\r\nOn Thu, Dec 1, 2011 at 11:17 PM, paulo alvarado\r\n<[email protected]>wrote:\r\n\r\n> test # 9\r\n>\r\n>\r\n> On Thu, Dec 1, 2011 at 11:15 PM, paulo alvarado <[email protected]\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>> [email protected]> wrote:\r\n>>\r\n>>> test # 7\r\n>>>\r\n>>> On Thu, Dec 1, 2011 at 10:36 PM, paulo alvarado <\r\n>>> [email protected]> 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>>>> [email protected]> 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>>>>> [email protected]> 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>>>>>> [email protected]> 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

编辑

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

如您所见,工单的描述包含电子邮件之前的回复

I am trying to retrieve messages from an email address using Imaplib, so far i have been able to retrieve the messages, however, i am only interested in the original message, meaning that if the message is a "Re:" then i don't want the previous messages, i want only the new one, is this possible ?

heres what i get from python :

test # 10\r\n\r\nOn Thu, Dec 1, 2011 at 11:17 PM, paulo alvarado\r\n<[email protected]>wrote:\r\n\r\n> test # 9\r\n>\r\n>\r\n> On Thu, Dec 1, 2011 at 11:15 PM, paulo alvarado <[email protected]\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>> [email protected]> wrote:\r\n>>\r\n>>> test # 7\r\n>>>\r\n>>> On Thu, Dec 1, 2011 at 10:36 PM, paulo alvarado <\r\n>>> [email protected]> 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>>>> [email protected]> 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>>>>> [email protected]> 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>>>>>> [email protected]> 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

as you can see test #10 is the new message, the other stuff are the previous replies
i am using python2.7

EDIT

I have a ticket application, so basically, i want the user to be able to submit tickets, via email, so the user's email is the author, the subject is the title, and the message body is the description, i have it all working fine except for how to handle a reply, because at this moment, if a user replies via email, the description of the ticket will not only contain the new issue ( or response ) but it will also contain the previous ones, so i want to know if there is a way to remove the previous responses and only keep the new one, heres a picture of my issue.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-12-26 15:34:29

Google 发现

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)') 是代码中的重要部分。

Google comes up with

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() 

where conn.search(None, '(UNSEEN)') is the important piece in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文