Mail-IMAPClient problem and suggestion needed

Discussion in 'Perl Misc' started by debhatta, Aug 28, 2006.

  1. debhatta

    debhatta Guest


    There are two issues for which I would like your opinion. First let me
    post a bit of code :

    use Mail::IMAPClient;
    use strict;
    use diagnostics;
    my $imap = Mail::IMAPClient->new(
    Server => $imap_server ,
    User => $user_id ,
    password => $password,
    Uid => 1, # Optional
    ) or die "cant connect to imap server, $!";
    my @msgs = $imap->search("ALL");
    foreach my $msg (@msgs) {
    print "This is $msg\n";
    my @flags = $imap->flags($msg) or die " could not flags:
    my $i;
    for ( $i=0;$i<@flags;$i++) {
    print "The flag is $flags[$i]\n";
    ...... etc.

    Say there is one message in the INBOX, then the o/p of the above code

    This is 26006
    Uncaught exception from user code:
    could not flags: at line 51.

    Can anyone tell me why its failing while retrieving the flags ? Is it
    because there are no flags set ?

    Secondly I want your opinion on my approach. I have a mail account
    where people send their queries and I reply back. Sometimes it leads to
    mail chains, for example, I can ask for more information etc. Finally
    when the issue is completed I move the mails to completed. Now, what I
    want is a auto-reply, which will be sent to every 'new' mail that is
    received. So I do some checks as to where it came from etc. and if it
    fits the criteria, I send an auto-reply and put the subject in the log
    file. Next time my auto-reply script runs in a cron and if it sees a
    mail, it does the same validations and checks whether the subj is
    present in the log file. If iyes, I assume its an old mail and do not
    send an auto-reply. It serves the purpose for most issues but fails if
    two people send a mail with same subject. One then misses the
    auto-reply. Can you suggest any simple mechanism without using any
    extra modules ( like the auto-reply berkely db - I cannot use a db ) ?
    Main problem is identifying whether the mail is a new one or seen
    before ? Note that I remove mails from the inbox every now and then. I
    am thinking of adding a flag to a message , which is not shown to the
    user by Outlook or mozilla and then read that. To that end I was trying
    the first part of my query.

    Sorry for the long mail. but any suggestions welcome.

    debhatta, Aug 28, 2006
    1. Advertisements

  2. debhatta

    J. Gleixner Guest

    ) or die "cant connect to imap server, [email protected]"; # Use [email protected] instead of $!
    You could avoid @msgs.

    for my $msn ( @{ $imap->search("ALL") } )
    and make $i only available within the for loop.

    for my $i ( 0 .. $#flags )
    Why not have it tell you?

    my @flags = $imap->flags($msg) or die "Could not get flags for msg:$msg:
    [email protected]"; # use [email protected] instead of $!
    ... I can't help you there..
    J. Gleixner, Aug 28, 2006
    1. Advertisements

  3. debhatta

    David Squire Guest

    Why not avoid $i altogether?

    foreach my $flag (@flags) {
    print "The flag is $flag\n";

    or even

    print "The flag is $_\n" for @flags;

    (These also avoids any potential pathological weirdness if someone has
    messed around with the array indexing start value)

    David Squire, Aug 28, 2006
  4. debhatta

    Thrill5 Guest

    Instead of just logging the subject, log both the subject and the from
    address of the messages.

    Thrill5, Aug 29, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.