Perl imap move message to trash (Mail :: IMAPClient)

I need to move all messages from invisible to trash (and remove them from inbox).

my $inbox = $imap->select("Inbox");
my @mails = ( $imap->unseen );

foreach my $msgid (@mails) {
    $imap->set_flag( "Deleted", @mails )
        or die "Could not set flag: $@\n";
}

      

This code completely removes messages. (without exception)

I tried using "move" and "copy":

my $Trash = "Trash";
my $newUid = $imap->move( $Trash, $msgid )
    or die "Could not move: $@\n";
my $uidList = $imap->copy( $Trash, @mails )
    or die "Could not copy: $@\n";

      

But "move" create a new label (folder) and "copy" doesn't work "Copy failed: 6 NO [TRYCREATE] No Trash folder (Failure)" I tried to use the name: / Trash, [imap] Trash, etc. . Similar results. This should work for different email services!

I use Mail::IMAPClient

+3


source to share


1 answer


Try below code for imap servers supporting RFC6154 like Gmail . It should determine the name of the Trash folder.



use  Mail::IMAPClient; 
 ......
my $Trash;
{
  my @Trash;
  my @fhashes = $imap->folders_hash or die "Could not get list of folder hashes.\n";
  foreach my $fhash (@fhashes) {
    next unless map { /^\\Trash$/ ? ($_) : () } @{$fhash->{attrs}};
    push (@Trash, $fhash->{name});
  }
  $Trash = pop( @Trash) if @Trash == 1;
}
if( defined( $Trash)) {
   ...
}   

      

+4


source







All Articles