Perl und IMAP

Das Modul Mail::IMAPClient ermoeglicht der Programmiersprache Perl den komfortablen Zugriff auf einen IMAP-Server.

Beispiel 1:

use strict;
use warnings;
use Mail::IMAPClient;

$imap = Mail::IMAPClient->new( Server  => ‘mail.server.com:143′,
                              User    => ‘me’,
                              Password  => ‘mypass’)
        # module uses eval, so we use $@ instead of $!
        or die "IMAP Failure: $@";

foreach my $box qw( HAM SPAM ) {
   # which file are the messages going into
   my $file = "mail/$box";

   # select the mailbox to get messages from
   $imap->select($box)
        or die "IMAP Select Error: $!";

   # store each message as an array element
   my @msgs = $imap->search(‘ALL’)
        or die "Couldn’t get all messagesn";

   # loop over the messages and store in file
   foreach my $msg (@msgs) {
     # pipe msgs through ‘formail’ so they are stored properly
     open my $pipe, "| formail >> $file"
       or die("Formail Open Pipe Error: $!");

     # send msg through file pipe
     $imap->message_to_file($pipe, $msg);

     # close the messgae pipe
     close $pipe
       or die("Formail Close Pipe Error: $!");
   }

   # close the folder
   $imap->close($box);
}

$imap->logout();

Beispiel 2:

use strict;
use warnings;
use Mail::IMAPClient;

$imap = Mail::IMAPClient->new( Server  => ‘mail.server.com:143′,
                              User    => ‘me’,
                              Password  => ‘mypass’)
        # module uses eval, so we use $@ instead of $!
        or die "IMAP Failure: $@";

foreach my $box qw( HAM SPAM ) {
   # how many msgs are we going to process
   print "There are ". $imap->message_count($box).
          " messages in the $box folder.n";

   # which file are the messages going into
   my $file = "mail/$box";

   # select the mailbox to get messages from
   $imap->select($box)
        or die "IMAP Select Error: $!";

   # store each message as an array element
   my @msgs = $imap->search(‘ALL’)
        or die "Couldn’t get all messagesn";

   # loop over the messages and store in file
   foreach my $msg (@msgs) {
     # pipe msgs through ‘formail’ so they are stored properly
     open my $pipe, "| formail >> $file"
       or die("Formail Open Pipe Error: $!");

     # skip the msg if its over 100k
     if ($imap->size($msg) > 100000) {
       $imap->delete_message($msg);
       next;
     }

     # send msg through file pipe
     $imap->message_to_file($pipe, $msg);

     # close the messgae pipe
     close $pipe
       or die("Formail Close Pipe Error: $!");

     # delete each message after downloading
     $imap->delete_message($msg);

     # If we are just testing and want to leave
     #  leave the messages untouched, then we
     #  can use the following line of code
     # $imap->deny_seeing($msg);
   }

   # expunge and close the folder
   $imap->expunge($box);
   $imap->close($box);
}

$imap->logout();

This entry was posted in Skripte. Bookmark the permalink.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>