Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

IMAP: mark message as unseen

by Arik123 (Beadle)
on Jul 03, 2018 at 10:01 UTC ( [id://1217806]=perlquestion: print w/replies, xml ) Need Help??

Arik123 has asked for the wisdom of the Perl Monks concerning the following question:

Consider the following code:

use Net::IMAP::Simple::SSL; use Email::MIME; my $imap = Net::IMAP::Simple::SSL -> new ('imap.gmail.com'); $imap -> login ("LOGIN\@gmail.com" => 'PASS'); my @unseen = reverse sort $imap->search_unseen; for my $i (@unseen) { my $msg = join '', @{$imap->get($i)}; unless (fork) { if (&process (Email::MIME->new (\$msg)) eq "notouch") { $imap->unsee ($i); exit; } } }

&process() should decide whether or not to mark the message as \Seen. however, it doesn't work. Whatever &process() returns, the message is marked as \Seen. I think it has to do with the exit() the child performes - maybe that forces the message to be \Seen.

Any solution? thanks!

Replies are listed 'Best First'.
Re: IMAP: mark message as unseen
by Corion (Patriarch) on Jul 03, 2018 at 11:02 UTC

    I think the \Seen flag is set automatically by the IMAP server whenever any client retrieves a message body. At least in my (automated) IMAP clients, I have resorted to programmatically remove the \Seen flag on mails that I retrieve. Looking at Mail::IMAPClient, there is mention of the Peek flag, which should let the IMAP server know that the mail should remain unread. I haven't tried this, but it could work like this:

    $imap->Peek(1); # don't mark mails as \Seen automatically my @unseen = reverse sort $imap->search_unseen; for my $i (@unseen) { my $msg = join '', @{$imap->get($i)}; unless (fork) { if (&process (Email::MIME->new (\$msg)) eq "notouch") { $imap->unsee ($i); exit; } } }

      Okay, the PEEK things works... temporarily (it isn't implemented in Net::IMAP::Simple::SSL, but I did it manually). After fetching the messages using the PEEK thing, the web interface shows that the messages are still unread. However, if I wait a few moments without any action, and then re-load the web interface, the messages are marked as Seen.

      Any idea what is going on? Thanks a lot!

        Sorry, it was probably my mistake.

        However, the PEEK method doesn't yet work perfectly (I'm trying to figure out why). Is there any alternative?

Re: IMAP: mark message as unseen
by Arik123 (Beadle) on Jul 13, 2018 at 06:49 UTC

    Okay, I seem to have solved this. The problem was with the fork(). Apparently the $imap isn't carried to the child process as it should have. I now implement it all in one thread, and it seems to work perfectly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217806]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found