Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

finding unread emails in IMAP maildir

by chrisj0 (Acolyte)
on Aug 08, 2005 at 19:50 UTC ( #481994=perlquestion: print w/ replies, xml ) Need Help??
chrisj0 has asked for the wisdom of the Perl Monks concerning the following question:

HI all,
I posted a question last week wondering about perl email modules. I needed to resend or forward a users entire maildir to another address. I got a very useful response from gellyfish (thank you) about using Email::Send and Email::Folder. I've useed these to create the program I needed.

Question: Now looking at what I need to use this for, I think It would be good to have the option to only resend/forward any unread/new messges rather than every message. I've looked at cpan for a module to do this (the Email::Folder can't seem to do it) but haven't found a module with this function. Is there another module for email I can use to do this? I can post code if it helps.
Thanks all,
chrisj

Comment on finding unread emails in IMAP maildir
Re: finding unread emails in IMAP maildir
by sgifford (Prior) on Aug 08, 2005 at 20:04 UTC
    Unread messages should be in the new subdirectory of Maildir, and read messages in cur. Not sure about using those modules, but that's how Maildir works. See http://cr.yp.to/proto/maildir.html.
      Yes I know,
      I've tried giving Email::Folder just a subdirectory of the Maildir, but it errors out saying it's not a maildir.
        Ah. A clever hack might be to create a fake Maildir with a symlink to cur and an empty new and tmp to get the read messages, and a symlink to new and an empty cur and tmp to get the unread messages.

        But maybe the best solution is to modify Email::Folder::Maildir to include this information, perhaps by prepending a fake header saying where the message came from.

      Unread messages should be in the new subdirectory of Maildir, and read messages in cur. Not sure about using those modules, but that's how Maildir works. See http://cr.yp.to/proto/maildir.html.
      That's basically correct but... at least with my Postfix / Courier IMAP / Squirrelmail setup, as soon as I use Squirrelmail any messages in the new dir are moved to cur - even if they're not read yet. When the message is read, :2,S is appended to the file name in cur. This is best explained at http://www.courier-mta.org/?maildir.html . Other MTA/IMAP server/MUA combinations may handle this differently.

Re: finding unread emails in IMAP maildir
by davidrw (Prior) on Aug 08, 2005 at 20:05 UTC
    Doe Mail::IMAPClient fit your needs? It's very easy to use..
    use Mail::IMAPClient; my $imap = Mail::IMAPClient->new( Server => $host, User => $id, Password=> $pass, ) or die "Cannot connect to $host as $id: $@"; $imap->select($folder); my @msgs = $imap->unseen(); foreach my $msgid( @msgs ){ ... }
      Thanks,
      I really don't want to make an IMAP connection to the server. I'd rather do this on at the filesystem. I'll look into the IMAPClient module. I have a feeling that you're right in saying the way to check for new messages is with an IMAP connection.
      Thanks again,
      chrisj
Re: finding unread emails in IMAP maildir
by xdg (Monsignor) on Aug 08, 2005 at 20:39 UTC

    Sometimes, Email::Simple and friends are too simple. I strongly suggest you look at Mail::Box. It looks big and complicated because of the incredibly complex class heirarchy, but read the Mail::Box::Cookbook and experiment with it and you'll find it's not terribly hard to use. Once you get used to it (and there is a big learning curve) it's a swiss-army-chainsaw for mail processing.

    Given your question, you probably want something like this (not tested):

    my $manager = Mail::Box::Manager->new; my $folder = $manager->open( folder => $ENV{MAIL} ); foreach my $message ( $folder->messages ) { next if $message->label( 'seen' ); my $bounce = $message->bounce( To => 'you@somewhere.com' ); $bounce->send; $message->label( 'seen' => 1 ); } $folder->close;

    Look at Mail::Message for other things you can do with a message (reply, forward, etc.) There's also very good online documentation with links for a mailing list that is very responsive to questions.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Yes, you're right about Email::Simple being too simple. I really didn't want to rewrite the whole thing, but I guess it's good practice.
      I'll take a look at the Mail::Box suite of modules and see how hard it would be to intregrate.
      Thanks for your help,
      chrisj

        One nice feature about Mail::Box is that it tries to make things as consistent as possible across backends. So, for example, $message->label( 'seen' ) is the same whether you're using Maildirs or IMAP, even though the way the two signal seen vs. unseen messages is very different. Shifting my example above to an IMAP routine instead of a Maildir routine probably only requires changing the way in which you call $manager->open( ... ) to include the right parameters for an IMAP type folder (server, user, password, etc.). (Assuming everything else is supported in the new type of folder, of course.)

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://481994]
Approved by sgifford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-09-20 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls