Receive ONLY a new email message

I am trying to receive messages from an email address using Imaplib, so far I have been able to receive messages, however I am only interested in the original message, which means that if the message is "Re:" then I do not want the previous messages, I want only new, is it possible?

Here is what I get from 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 

as you can see test # 10 is a new post, others are previous answers I am using python2.7

EDIT

I have a ticket application, so basically, I want the user to be able to send tickets by email, so the user’s email address is the author, the subject is the header, and the message body is the description, I have everything working fine, except how to handle the answer, because at this moment, if the user answers by email, the ticket description will not only contain a new problem (or answer), but also will contain the previous ones, so I want to know if there is especially delete the previous answers and save only the new one, here is an image of my problem.

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

+4
source share
1 answer

Google offers

 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 an important part of the code.

-1
source

Source: https://habr.com/ru/post/1384643/


All Articles