Have been trying to create a script to strip an email of all binary attachments. The test email has 25 parts, encompassing 24 photos as attachments, which leaves the email headers and the first 'part', the plain text body.

#!/usr/bin/env perl # use strict; use warnings; use Data::Dumper; use File::Slurp qw( read_file ); use Email::Address; use Email::MIME::Attachment::Stripper; use File::Slurp qw(slurp write_file); my $infile = '/home/****/Mail/.family.directory/1277709595.16641.pRLS +b:2,S'; my $outfile = 'output4.txt'; my $intext = File::Slurp::read_file( $infile ); my $stripper = Email::MIME::Attachment::Stripper->new($intext); my $email_mime = $stripper->message; print $email_mime; # displays "Email::MIME=HASH(0x1601e60)" my @emails = Email::Address->parse( $email_mime ); File::Slurp::write_file( $outfile, join("\n", @emails) ); # no outp +ut ? my $parsed = Email::MIME->new($intext); #print "Parsed content :\n". Dumper( $parsed) . "\n"; my $parts = $parsed->parts; print "Number of email parts : $parts\n"; # displays "Number of ema +il parts : 25" my @parts = $parsed->parts; foreach my $attachment ( $stripper->attachments ) { write_file( $attachment->{filename}, { buf_ref => \$attachment->{payload} } ) or die "Can't write $attachment->{filename}: $!\n"; }

The code just above (the "write_file")works okay, and all 24 photos are extracted and written to disk. But the part I want to process is the email headers plus the first part. Have been looking at the docs for this module, and it seems all I have to do is ..

my $stripper = Email::MIME::Attachment::Stripper->new($intext); my $email_mime = $stripper->message;

to get the message part. I can't get it to extract the first part, the 'message' though. Have even downloaded the source for this module; the sub 'message' does a 'detach_all. Any clues please ?


In reply to Stripping emails of attachments by peterr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.