打开APP
userphoto
未登录

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

开通VIP
Retrieving email and decoding attachments
userphoto

2012.06.09

关注

Retrieving email and decoding attachments

by jtapper
on Sep 25, 2002 at 21:11 UTC ( #200742=perlquestion: print w/ replies, xml ) Need Help??
jtapper has asked for thewisdom of the Perl Monks concerning the following question:

I am interested in writing a perl script that will log onto my pop3 account and retrieve all the email, and save any attachments into a local directory.

I've already written something to check my email but I have no idea how to implement retrieving and saving attachments.

can somebody point me in the right direction?thanks

Comment onRetrieving email and decoding attachments
Re: Retrieving email and decoding attachments
by trs80 on Sep 25, 2002 at 21:43 UTC
    The Mail::MboxParser module offers a nice interface to achieve this.

    MIME::Parser is one of the most robust mail parsing moudles that you may also want to consider, but it has a steep learning curve in my opinion.
    Update As valdez points out you will need something like Mail::POP3Client to get the mails, I misread your post and thought for some strange reason you already had the messages locally.
Re: Retrieving email and decoding attachments
by valdez on Sep 25, 2002 at 22:32 UTC
Re: Retrieving email and decoding attachments
by jlongino on Sep 26, 2002 at 06:56 UTC
    This script should do what you want. I looked at several modules trying to find one that does everything, but lately I find that most mail related modules do one or two very specific things well, but for anything but the simplest tasks end up using more than one to get the job done. In this case, Mail::POP3Client is a good POP3 access tool but MIME::Parser is hard to beat for saving attachments to disk:
    #!/usr/local/bin/perluse strict;use warnings;use Mail::POP3Client;use MIME::Parser;my $pop = new Mail::POP3Client( USER => "user", PASSWORD => "password", HOST => "pop3.server");## for HeadAndBodyToFile() to usemy $fh = new IO::Handle();## Initialize stuff for MIME::Parser;my $outputdir = "./mimemail";my $parser = new MIME::Parser;$parser->output_dir($outputdir);my $i;## process all messages in pop3 inboxfor ($i = 1; $i <= $pop->Count(); $i++) { open (MAILOUT, ">pop3.msg$i"); $fh->fdopen( fileno( MAILOUT ), "w" ); ## write current msg to file $pop->HeadAndBodyToFile( $fh, $i ); close MAILOUT; ## MIME::Parser handles only one msg at-a-time open (MAILIN, "<pop3.msg$i"); ## flush all attachments this msg to ./mimemail dir using internal +filename my $entity = $parser->read(\*MAILIN); close MAILIN;}
    BTW, this was done hastily and could use more error testing, could probably be done more simply/elegantly, but I leave that for the poster.

    Does anyone know how to read the messages directly to MIME::Parser without having to write the intermediate files first? I've encountered this dilemma recently trying to combine other unrelated mail modules.

    --Jim

      This works too and is a bit simpler.
      for ($i = 1; $i <= $pop->Count(); $i++) { print "$i \n"; my $msg; $msg = $pop->HeadAndBody( $i ); my $entity = $parser->parse_data($msg);}
Re: Retrieving email and decoding attachments
by TuftyTrue on Jun 29, 2008 at 09:45 UTC
    Thansk guys,I've got it up and working now with the library you recommended!TaTT
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
18.1.11. email: Examples — Python v2.7.1 docu...
zabbix企业应用之定时获取监控数据做报表
短语分享 | on one's way & pop the question
邮件发送的原理
The Lucene search engine: Powerful, flexible, and free
Why I Switched to SuperMemo After Using Anki for 5 Years, With Over 50k Cards and 420k Total Reviews
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服