kaiux

Simple Script to Download Email from a IMAP Server

Mar 16th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.90 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # @kaiux
  3.  
  4. use IMAP::Client;
  5. use Data::Dumper;
  6. use strict;
  7. use warnings;
  8.  
  9. my $server = 'imap.server.com';
  10. my $user = 'user@domain';
  11. my $pass = 'YourStrongPass';
  12.  
  13. my $imap = new IMAP::Client();
  14. $imap->connect(
  15.     PeerAddr => $server,
  16.     ConnectMethod => 'SSL STARTTLS PLAIN',
  17. )
  18.     or die "Unable to connect to [$server]: ".$imap->error();
  19.  
  20. $imap->onfail('ERROR');
  21. $imap->errorstyle('STACK');
  22. #$imap->debuglevel(1);
  23. $imap->capability_checking(1);
  24.  
  25. $imap->authenticate($user,$pass)
  26.     or die "Unable to authenticate as $user ".$imap->error()."\n";
  27.  
  28. $imap->select("INBOX");
  29. #my @all_uids = $imap->uidsearch('ALL'); #change here and to uidfetch from fetch
  30. my @all_uids = $imap->search('ALL'); #this is the sequence from newest to oldest
  31.  
  32. foreach my $msg_id (@all_uids) {
  33.     my %content = $imap->fetch($msg_id,{});
  34.     print $content{$msg_id}->{BODY}->{BODY}, "\n";
  35. }
  36.  
  37. $imap->disconnect();
Add Comment
Please, Sign In to add comment