Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

mime parser multiple emails

by jjoerg (Initiate)
on Mar 27, 2019 at 17:43 UTC ( #1231756=perlquestion: print w/replies, xml ) Need Help??

jjoerg has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I know there is something very basic that your deep knowledge and wisdom will see immediately. I am a broken and imperfect vessel and cannot see. Please help me.

I get many emails with attachments to a user account that need automated extracting on a regular basis. They come in at irregular times, and the mail gets checked 1x/month or so. I have not used MIME::Parser before, and wrote the following:

use MIME::Parser; $mailfile = "$mailspool/[username]"; $parser = new MIME::Parser; $head = new MIME::Head; open(IN, "$mailfile"); $parser->output_under("$basedir/INCOMING"); $entity = $parser->parse(\*IN);

This works nicely, but only for the first message in the mailbox. The attachments of that message get saved and the program exits. How do I keep it going through all the messages in the mail spool?

I thank you for your kind assistance.

Replies are listed 'Best First'.
Re: mime parser multiple emails
by roboticus (Chancellor) on Mar 27, 2019 at 18:06 UTC


    That's because you're expecting a bit too much from MIME::Parser--it wants a thing with MIME in it, and you're passing it an entire mailbox. Try using a module to parse the mailbox into individual messages (such as Mail::Box or similar). Then you can examine the EMail to see if you've processed it or not. For the ones you haven't, call the MIME::Parser to do your extractions.


    When your only tool is a hammer, all problems look like your thumb.

Re: mime parser multiple emails
by thanos1983 (Parson) on Mar 28, 2019 at 08:34 UTC

    Hello jjoerg,

    Welcome to the Monastery. Fellow Monk roboticus already answered your question but I wanted to add something small in case that can help you move with your task and possibly get more information on the process. There is a really nice "Tutorial" and sample of code on the Monastery, have you read this Parsing emails with attachments?

    From my point of view this all you need to get you going :). If this is not enough why not use another module as well Email::Simple?

    Update: Removing wrong recommendation of module. Thanks to fellow Monk parv for noticing.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      I do not see how Email::Simple would help OP as it (a) works with a single message and (b) gives headers & body; does not have the capability to extract an attachment.

        Hello parv,

        You are right, I skipped checking further if Email::Simple can parse the attachment. Thanks for pointing out :).

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231756]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2021-05-16 19:10 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (152 votes). Check out past polls.