Mail-IMAPClient problem and suggestion needed

D

debhatta

Hi

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, $!";
$imap->select("INBOX");
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
is

This is 26006
Uncaught exception from user code:
could not flags: at auto2.pl 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.


Thanks,
Raj.
 
J

J. Gleixner

Hi

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, $!";

) or die "cant connect to imap server, $@"; # Use $@ instead of $!
$imap->select("INBOX");
my @msgs = $imap->search("ALL");
foreach my $msg (@msgs) {

You could avoid @msgs.

for my $msn ( @{ $imap->search("ALL") } )
print "This is $msg\n";
my @flags = $imap->flags($msg) or die " could not flags:
$!";
my $i;
for ( $i=0;$i<@flags;$i++) {

and make $i only available within the for loop.

for my $i ( 0 .. $#flags )
print "The flag is $flags[$i]\n";
}
..... etc.

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

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

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

Why not have it tell you?

my @flags = $imap->flags($msg) or die "Could not get flags for msg:$msg:
$@"; # use $@ instead of $!
Secondly I want your opinion on my approach.
... I can't help you there..
 
D

David Squire

J. Gleixner said:
(e-mail address removed) wrote:
$!";
my $i;
for ( $i=0;$i<@flags;$i++) {

and make $i only available within the for loop.

for my $i ( 0 .. $#flags )
print "The flag is $flags[$i]\n";
}
..... etc.

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)


DS
 
T

Thrill5

Hi

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, $!";
$imap->select("INBOX");
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
is

This is 26006
Uncaught exception from user code:
could not flags: at auto2.pl 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.


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

Scott
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top