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

First off.. I hate this stuff.. Second.. I looked around and found the code on this site. I've been messing with this same code, and others like it for about a day now and am getting frustrated. Whenever these scripts receive emails that have attachments they think the body is the body of the attachment. I have yet to found one that works. I can output the whole message, but that includes a bunch of garbage.. So here is the code, please let me know if anyone has anyway to get this to work properly with attachments, or knows of a place where I can find this please email me! Thanks! Code found at: http://perlmonks.thepen.com/195442.html James Woods woodsjr@purdue.edu

Replies are listed 'Best First'.
Re: Piping Email to Perl with attachments
by nite_man (Deacon) on Jul 23, 2003 at 07:51 UTC

    You can use Mail::Sender for sending emails with attachments.
    I've just put simple example from my project for sending PDF file as attachment.
    See also Mail::Sender documentation for more details and examples.

    my $body = 'This is a body of your message!'; # Define a hash with mail data my $data_href = { to => 'recipients mails', body => \$body, + ctype => 'text/plain', smtp => 'your smtp server', from => 'your email addr', replyto => 'reply to addr', + subject => 'Your subject', }; my $sender = new Mail::Sender() or die "Cannot create sender objec +t!"; $sender->OpenMultipart( { %$data_href, encoding => "quoted-printable", debug => '/your/place/for/log fil +e', } ); warn "Error in mailing : $Mail::Sender::Error\n" unless ref $sende +r; $sender->Body( { msg => ${$data_href->{body}}, ctype => $data_href->{ctype}, encoding=> 'Base64', } ); $sender->Attach( { file => 'your attach file', description => 'Your attach description', ctype => 'your attach file mime type', encoding => 'Base64', disposition => "attachment; file='your attach + file'; type='PDF'", } ); $sender->Close; if(defined $sender->{error}) { warn "Error of sending mail to '$$data_href{to}': ".$sender-> +{error}."!!!" } else { warn "Mail was sent to the '$$data_href{to}'!" }

    Also, there is another way for building email messages - use MIME::Entity. It's a part of MIME::tools library. Actually, I don't use this module, but I've seen it in some application and heard that it's a good solution.

    Hope I helped.

          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: Piping Email to Perl with attachments
by bobn (Chaplain) on Jul 23, 2003 at 02:43 UTC

    Check out the MIME::Parser module - this makes it nearly trivial to write a script to parse email attachments.

    Update: ARRGGHHH! Didn;t notice the node he linked was using MIME::Parser. Still, that script looks like it was making it harder than needed. I've used this module and it is dead simple.

    --Bob Niederman, http://bob-n.com