打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Mail::IMAPClient | Erics Tech Blog
userphoto

2012.06.09

关注

Mail::IMAPClient

Description: Recently, I have had the pleasure of getting knee deep into various aspects of Email. One of the things that I consistantly found myself wanting to do was to parse through it. I know the best way to do this is to connect to the IMAP server and download the messages. The best way I have come accross on how to accomplish this task is using Mail::IMAPClient.

CPAN: Mail::IMAPClient

Example 1:
Creating a connection to an IMAP server isn’t complicated. And once you are connected there are many things that you can do to manipulate messages. In this first example, I am merely going to show you how to connect to the mail server and download ALL the messages in specific folders:

# Always be safeuse strict;use warnings;# Use the moduleuse 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 messages\n";   # 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); } # We're all done with IMAP here $imap->logout();

Example 2:
In this next example, we are going to take advantage of some other methods that are provided by this useful little module. We will begin by using the same base as is in Example 1, but we will add some nuances in the middle for functionality.

Note: Messages don’t get immediately deleted with IMAP, only marked for deletion. They aren’t actually deleted until the box is expunged. In this case, it gets done after the looping over each mailbox is complete. This is to say that if the program gets interrupted in the middle that the messages won’t be deleted until the mailbox is officially issued an expunge command.

# Always be safeuse strict;use warnings;# Use the moduleuse 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 messages\n";   # 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); } # We're all done with IMAP here $imap->logout();

Related posts:

  1. Mail::Sender
  2. Backing Up Gmail/Google Apps to a Dovecot Server
  3. Transferring Email From Gmail/Google Apps to Dovecot With Larch
  • http://eric.lubow.org/ eric

    I would check out the $imap->search() method (http://search.cpan.org/~plobbes/Mail-IMAPClient-3.23/lib/Mail/IMAPClient.pod#search). After that, you can iterate over the message ids returned by the search method, grab them, programattically figure out if they are after that date and do with them what you want. Or you can get a list of messages that match the subject and a list of messages that are after the date and compare the message ids. Both of these are done with the $imap->search() method.

  • sandy2010

    I would like to know how to search for messages with a particular subject and after a particular date.

  • M Hopcroft

    Thanks for this, it was helpful. Let me add that using SSL with an IMAP server is not directly supported by Mail::IMAPClient, but it can be done pretty easily with the “Socket” method. See http://gagravarr.org/code/ for an example.

  • Glenn

    Try this site: http://aspn.activestate.com/ASPN/CodeDoc/Mail-I…

    $imap->Uid(1);
    my @uids = $imap->search('SUBJECT',”Just a silly test”); #
    $imap->Uid(0);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JavaMail 深入浅出
邮件发送的原理
delphi 之 WebBorwser 解决无法模拟Enter
Messages
j2ee1.4 mail --5.0??
当SSH遇到“Write failed: Broken pipe” | 落格博客
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服