Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python imaplib doesnt mark messages as unseen

Tags:

python

imaplib

I am working on a function that collects data from email and has a switch to mark messages as unseen. During development it started to fail I don't know why. I've looked it up in documentation, I've searched stackoverflow (got to this thread, but it didn't help). Anyway. This is the code:

    mail = imaplib.IMAP4_SSL('imap.gmail.com', '993')
    mail.login(settings.INVOICES_LOGIN, settings.INVOICES_PASSWORD)
    mail.select('inbox')

    result, data = mail.uid('search', '(UNSEEN)', 'X-GM-RAW',
                            'SUBJECT: "{0}" FROM: "{1}"'.format(attachment_subject, attachment_from))
    uids = data[0].split()
    for uid in uids:
        result, data = mail.uid('fetch', uid, '(RFC822)')
        m = email.message_from_string(data[0][1])

        if m.get_content_maintype() == 'multipart':
            for part in m.walk():
                if part.get_content_maintype() == 'multipart':
                    continue
                if part.get('Content-Disposition') is None:
                    continue
                if re.match(attachment_filename_re, part.get_filename()):
                    attachments.append({'uid': uid, 'data': part.get_payload(decode=True)})

        if set_not_read:
            mail.store(uid, '-FLAGS', '(\Seen)')

I've debugged it, I am sure that with this flag the mail.store(uid, '-FLAGS', '(\Seen)') part is entered, I've also tried switching to \SEEN and \Seen instead of (\Seen).

EDIT:

What I'm trying to do is to make a script that allows user to mark email as unseen (not read), this is reset the Seen flag, and not to allow it to mark an email as seen (read).

like image 922
gonczor Avatar asked May 07 '26 07:05

gonczor


1 Answers

I believe you want

mail.store(uid, '+FLAGS', '(\\Seen)')

I think what you're doing right now is removing the seen flag. But I'll look in the RFC to be sure.

Edit: Yup. That's what the RFC says

-FLAGS <flag list>
         Remove the argument from the flags for the message.  The new
         value of the flags is returned as if a FETCH of those flags was
         done.

Other bits that you may find relevant:

The currently defined data items that can be stored are:

      FLAGS <flag list>
         Replace the flags for the message (other than \Recent) with the
         argument.  The new value of the flags is returned as if a FETCH
         of those flags was done.

      FLAGS.SILENT <flag list>
         Equivalent to FLAGS, but without returning a new value.

      +FLAGS <flag list>
         Add the argument to the flags for the message.  The new value
         of the flags is returned as if a FETCH of those flags was done.

      +FLAGS.SILENT <flag list>
         Equivalent to +FLAGS, but without returning a new value.

      -FLAGS <flag list>
         Remove the argument from the flags for the message.  The new
         value of the flags is returned as if a FETCH of those flags was
         done.

      -FLAGS.SILENT <flag list>
         Equivalent to -FLAGS, but without returning a new value.
like image 70
Wayne Werner Avatar answered May 09 '26 22:05

Wayne Werner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!