使用Net :: IMAP :: Simple移动gmail消息

时间:2015-06-29 14:05:44

标签: perl

我尝试使用Net::IMAP::Simple将邮件移动到old_messages文件夹后,我已经阅读并从中剥离了附件,但是当我这样做时,所有移动的邮件都是空白的,并且来自< em>&#34;未知发件人&#34; ,收件箱未更改。我已经检查过,之前似乎没有人遇到过这个问题。

我也使用Email::SimpleEmail::MIME作为$es对象在语句中作为参数传递

$imap->put( 'OLD_MESSAGES', $es, "") or warn $imap->errstr

但都没有奏效。

这是我使用Email::MIME

的代码
use strict;
use warnings;

# required modules
use Net::IMAP::Simple;
use Email::MIME;
use IO::Socket::SSL;
use Email::MIME::Attachment::Stripper;

# fill in your details here
my $username = 'usersite.com';
my $password = 'password';
my $mailhost = 'imap.gmail.com';

# Connect
my $imap = Net::IMAP::Simple->new( $mailhost, port => 993, use_ssl => 1, )
    || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";

# Log in
if ( !$imap->login( $username, $password ) ) {
    print STDERR "Login failed: " . $imap->errstr . "\n";
    exit( 64 );
}

# Look in the the INBOX
my $nm = $imap->select( 'INBOX' );

# How many messages are there?
my ( $unseen, $recent, $num_messages ) = $imap->status();
print "unseen: $unseen, recent: $recent, total: $num_messages\n\n";

my $filepath = "C:/Users/doug/Desktop/gmail/";

## Iterate through unseen messages
for ( my $i = 1 ; $i <= $nm ; $i++ ) {

    if ( !$imap->seen( $i ) ) {
        next;
    }
    else {
        my $es = Email::MIME->new( join '', @{ $imap->get( $i ) } );

        #my $es = Email::MIME->new( join '', @{ $imap->top($i) } );
        my $text        = $es->body;
        my $stripper    = Email::MIME::Attachment::Stripper->new( $es );
        my @attachments = $stripper->attachments;

        printf(
            "[%03d] %s\n\t%s\n%s",
            $i,
            $es->header( 'From' ),
            $es->header( 'Subject' ), $text
        );
        my $l = 0;
        foreach $_ ( @attachments ) {

            my $fh = IO::File->new();
            binmode( $fh );
            open( $fh, '>', "$filepath" . "$_->{filename}" );
            print $fh "$_->{payload}\n";
            $fh->close;

        }
        $imap->put( 'OLD_MESSAGES', $es, "" ) or warn $imap->errstr;
    }
}

# Disconnect
$imap->quit;

exit;

1 个答案:

答案 0 :(得分:0)

我不得不删除我的代码,但下面是它的要点。在我剪掉它并粘贴在这里之前为我工作,知道它是否有效,我会编辑它

use Mail::IMAPClient;
use Email::MIME::Attachment::Stripper;
use other stuff as needed....

# login
my $sock = IO::Socket::SSL->new(PeerAddr=>'imap.gmail.com',PeerPort=>993);
my $imap = Mail::IMAPClient->new(Socket => $sock, User => $user, Password => $pass, Ignoresizeerrors => 1);
die $imap->LastError() unless $imap->IsAuthenticated();

# decoding mime (you can probably skip)
my $message = $imap->message_string(123);
my $decoded_mime = "";
my @decoded_list = MIME::Words::decode_mimewords($message);
for(my $i=0; $i<scalar(@decoded_list); ++$i) {
  my @set = @{$decoded_list[$i]};
  if(scalar(@set) == 1) {
    $decoded_mime .= $set[0];
  }
  else {
    eval {
      Encode::from_to($set[0],$set[1],'utf-8');
    };
    if($@) { eval {}; }
    $decoded_mime .= $set[0];
  }
}
$message = $decoded_mime;

# Strip attachments      
$mime = Email::MIME::Attachment::Stripper->new($message);
$mime = $mime->message; # convert to regular Email::MIME